예제 #1
0
        public bool HasBuilderPullSecret(string ocNamespace, string server)
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get secret -o json --namespace {ocNamespace}");

            if (!result.IsSuccess)
            {
                throw new FailedException($"Cannot get secrets: {result.ErrorMessage}");
            }
            foreach (var item in result.Value["items"])
            {
                string secretType       = (string)item["type"];
                string dockerCfgEncoded = null;
                if (secretType == "kubernetes.io/dockercfg")
                {
                    dockerCfgEncoded = (string)item?["data"]?[".dockercfg"];
                }
                else if (secretType == "kubernetes.io/dockerconfigjson")
                {
                    dockerCfgEncoded = (string)item?["data"]?[".dockerconfigjson"];
                }
                if (dockerCfgEncoded != null)
                {
                    string dockerCfg = Encoding.UTF8.GetString(Convert.FromBase64String(dockerCfgEncoded));
                    if (dockerCfg.Contains($"\"{server}\""))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #2
0
        public ReplicationController GetReplicationController(string deploymentConfigName, string version, bool mustExist)
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get rc -l openshift.io/deployment-config.name={deploymentConfigName} -o json");

            if (result.IsSuccess)
            {
                foreach (var item in result.Value["items"])
                {
                    ReplicationController rc = ReplicationControllerParser.Parse(item as JObject);
                    if (rc.Version == version)
                    {
                        return(rc);
                    }
                }
                if (!mustExist)
                {
                    return(null);
                }
                throw new FailedException("Replication controller not found");
            }
            else
            {
                if (!mustExist && result.ErrorMessage.Contains("NotFound"))
                {
                    return(null);
                }
                throw new FailedException($"Cannot get replication controller: {result.ErrorMessage}");
            }
        }
예제 #3
0
        private void RunCommand(string command, string ocNamespace, JObject value)
        {
            Result result = ProcessUtils.Run("oc", command + " " + NamespaceArg(ocNamespace) + " -f -", value);

            if (!result.IsSuccess)
            {
                throw new FailedException($"Unable to '{command}': {result.ErrorMessage}");
            }
        }
예제 #4
0
        public void CreateImageStream(string name)
        {
            Result result = ProcessUtils.Run("oc", $"create is {name}");

            if (!result.IsSuccess)
            {
                throw new FailedException($"Error creating image stream: {result.ErrorMessage}");
            }
        }
예제 #5
0
        public void EnsureConnection()
        {
            Result result = ProcessUtils.Run("oc", "whoami");

            if (!result.IsSuccess)
            {
                throw new FailedException($"Cannot connect to OpenShift cluster: {result.ErrorMessage}");
            }
        }
예제 #6
0
        public Result GetLog(string podName, string container, Action <StreamReader> reader, bool follow, bool ignoreError)
        {
            Result result = ProcessUtils.Run("oc", $"logs {(follow ? "-f" : string.Empty)} {podName} {(string.IsNullOrEmpty(container) ? "" : "-c " + container)}", reader);

            if (!ignoreError && !result.IsSuccess)
            {
                throw new FailedException($"Cannot get pod log: {result.ErrorMessage}");
            }
            return(result);
        }
예제 #7
0
        public static string GetSdkVersion()
        {
            Result <string> result = ProcessUtils.Run <string>("dotnet", "--version");

            if (result.IsSuccess)
            {
                return(result.Value.Trim());
            }
            else
            {
                return(null);
            }
        }
예제 #8
0
        public string GetCurrentNamespace()
        {
            // TODO: cache this
            Result <string> result = ProcessUtils.Run <string>("oc", "project -q");

            if (result.IsSuccess)
            {
                return(result.Value.Trim());
            }
            else
            {
                throw new FailedException($"Cannot determine current project: {result.ErrorMessage}");
            }
        }
예제 #9
0
        public static string GetHeadCommitId(string gitRoot)
        {
            Result <string> result = ProcessUtils.Run <string>("git", $"rev-parse HEAD", new ProcessStartInfo {
                WorkingDirectory = gitRoot
            });

            if (result.IsSuccess)
            {
                return(result.Value.Trim());
            }
            else
            {
                return(null);
            }
        }
예제 #10
0
        public static string GetCurrentBranch(string gitRoot)
        {
            Result <string> result = ProcessUtils.Run <string>("git", $"rev-parse --abbrev-ref HEAD", new ProcessStartInfo {
                WorkingDirectory = gitRoot
            });

            if (result.IsSuccess)
            {
                return(result.Value.Trim());
            }
            else
            {
                return(null);
            }
        }
예제 #11
0
        public static string GetRemoteUrl(string gitRoot, string remoteName)
        {
            Result <string> result = ProcessUtils.Run <string>("git", $"remote get-url {remoteName}", new ProcessStartInfo {
                WorkingDirectory = gitRoot
            });

            if (result.IsSuccess)
            {
                return(result.Value.Trim());
            }
            else
            {
                return(null);
            }
        }
예제 #12
0
        public Route[] GetRoutes()
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get route -o json");

            if (result.IsSuccess)
            {
                var routes = new List <Route>();
                foreach (var item in result.Value["items"])
                {
                    routes.Add(RouteParser.ParseRoute(item as JObject));
                }
                return(routes.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get routes: {result.ErrorMessage}");
            }
        }
예제 #13
0
        public Build[] GetBuilds(string buildConfigName)
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get builds -l openshift.io/build-config.name={buildConfigName} -o json");

            if (result.IsSuccess)
            {
                var builds = new List <Build>();
                foreach (var item in result.Value["items"])
                {
                    builds.Add(BuildParser.ParseBuild(item as JObject));
                }
                return(builds.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get build information: {result.ErrorMessage}");
            }
        }
예제 #14
0
        public DeploymentConfig[] GetDeploymentConfigs()
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get dc -o json");

            if (result.IsSuccess)
            {
                var deployConfigs = new List <DeploymentConfig>();
                foreach (var item in result.Value["items"])
                {
                    deployConfigs.Add(DeploymentConfigParser.ParseDeploymentConfig(item as JObject));
                }
                return(deployConfigs.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get deployment configurations: {result.ErrorMessage}");
            }
        }
예제 #15
0
        public Service[] GetServices()
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get svc -o json");

            if (result.IsSuccess)
            {
                var services = new List <Service>();
                foreach (var item in result.Value["items"])
                {
                    services.Add(ServiceParser.ParseService(item as JObject));
                }
                return(services.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get services: {result.ErrorMessage}");
            }
        }
예제 #16
0
        public void CreateBuilderPullSecret(string ocNamespace, string name, string server, string username, string password)
        {
            Result result = ProcessUtils.Run("oc", $"create secret docker-registry {name} --namespace {ocNamespace} --docker-server {server} --docker-username={username} --docker-password {password} --docker-email=unused");

            if (!result.IsSuccess)
            {
                throw new FailedException($"Cannot create secret: {result.ErrorMessage}");
            }
            result = ProcessUtils.Run("oc", $"secret link default {name} --namespace {ocNamespace} --for=pull");
            if (!result.IsSuccess)
            {
                throw new FailedException($"Cannot link secret for pull: {result.ErrorMessage}");
            }
            result = ProcessUtils.Run("oc", $"secret link builder {name} --namespace {ocNamespace} ");
            if (!result.IsSuccess)
            {
                throw new FailedException($"Cannot link secret for builder: {result.ErrorMessage}");
            }
        }
예제 #17
0
        public Pod GetPod(string podName, bool mustExist)
        {
            if (string.IsNullOrEmpty(podName))
            {
                throw new ArgumentException(nameof(podName));
            }
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get pod {podName} -o json");

            if (result.IsSuccess)
            {
                return(PodParser.ParsePod(result.Value));
            }
            else
            {
                if (!mustExist && result.ErrorMessage.Contains("NotFound"))
                {
                    return(null);
                }
                throw new FailedException($"Cannot get pod information: {result.ErrorMessage}");
            }
        }
예제 #18
0
        public ImageStreamTag[] GetImageTagVersions(string name, string ocNamespace)
        {
            string           arguments = $"get is -o json {NamespaceArg(ocNamespace)} {name}";
            Result <JObject> result    = ProcessUtils.Run <JObject>("oc", arguments);

            if (result.IsSuccess)
            {
                return(ImageStreamParser.GetTags(result.Value));
            }
            else
            {
                if (result.ErrorMessage.Contains("NotFound"))
                {
                    return(Array.Empty <ImageStreamTag>());
                }
                else
                {
                    throw new FailedException($"Unable to retrieve image stream tags: {result.ErrorMessage}");
                }
            }
        }
예제 #19
0
        public Pod[] GetPods(string deploymentConfigName, string version)
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get pods -l deploymentconfig={deploymentConfigName} -o json");

            if (result.IsSuccess)
            {
                var pods = new List <Pod>();
                foreach (var item in result.Value["items"])
                {
                    var pod = PodParser.ParsePod(item as JObject);
                    if (version == null || pod.DeploymentConfigLatestVersion == version) // TODO: version == null?
                    {
                        pods.Add(pod);
                    }
                }
                return(pods.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get pod information: {result.ErrorMessage}");
            }
        }
예제 #20
0
        public S2iBuildConfig[] GetS2iBuildConfigs(string imageName)
        {
            Result <JObject> result = ProcessUtils.Run <JObject>("oc", $"get bc -o json");

            if (result.IsSuccess)
            {
                var buildConfigs = new List <S2iBuildConfig>();
                foreach (var item in result.Value["items"])
                {
                    var buildConfig = BuildConfigParser.ParseS2iBuildConfig(item as JObject);
                    if (buildConfig != null && buildConfig.ImageName == imageName)
                    {
                        buildConfigs.Add(buildConfig);
                    }
                }
                return(buildConfigs.ToArray());
            }
            else
            {
                throw new FailedException($"Cannot get build configurations: {result.ErrorMessage}");
            }
        }