/// <summary>
        /// Gets configuration components based off current context.
        /// </summary>
        /// <param name="config">The <see cref="KubeConfig"/>.</param>
        /// <param name="cluster">The <see cref="Cluster"/>.</param>
        /// <param name="user">The <see cref="User"/>.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static bool TryGetCurrentContext(KubeConfig config, out Cluster cluster, out User user)
        {
            cluster = default(Cluster);
            user    = default(User);

            if (null == config)
            {
                return(false);
            }

            Context context = config.Contexts.FirstOrDefault(p => p.Name == config.CurrentContext);

            cluster = config.Clusters.FirstOrDefault(p => p.Name == context.ContextData.Cluster);
            user    = config.Users.FirstOrDefault(p => p.Name == context.ContextData.User);
            return(true);
        }
        /// <summary>
        /// Parses kubernetes config from specified path to <see cref="KubeConfig"/> object.
        /// </summary>
        /// <returns>The <see cref="KubeConfig"/>.</returns>
        public static KubeConfig ParseKubeConfig(string path = null)
        {
            string kubeConfigFilePath = path ?? Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile, Environment.SpecialFolderOption.None), KubeConfigUtils.KUBE_CLIENT_CONFIG_PATH);
            FileInfo kubeConfigFileInfo = new FileInfo(kubeConfigFilePath);

            if (!kubeConfigFileInfo.Exists)
            {
                throw new Exception(string.Format("Kubernetes config file does not exist in path: {0}", kubeConfigFileInfo.FullName));
            }

            KubeConfig config = default(KubeConfig);

            using (StreamReader reader = File.OpenText(kubeConfigFileInfo.FullName))
            {
                config = new Deserializer().Deserialize <KubeConfig>(reader);
            }
            return(config);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new instance of <see cref="IKubernetesClient"/>.
        /// </summary>
        /// <param name="authType">The authentication type.</param>
        /// <returns>Instance of <see cref="IKubernetesClient"/>.</returns>
        public IKubernetesClient CreateClient(AuthType authType)
        {
            // Parse kube config from path
            KubeConfig kubeConfigFile = KubeConfigUtils.ParseKubeConfig(this.kubeConfigFilePath);

            // Get current context from kube config
            Cluster cluster = default(Cluster);
            User    user    = default(User);

            if (!KubeConfigUtils.TryGetCurrentContext(kubeConfigFile, out cluster, out user))
            {
                throw new ApplicationException("Invalid config in %USERPROFILE%\\.kube\\config.");
            }

            // Build client
            ApiClient client = new ApiClient(cluster.ClusterData.Server);

            switch (authType)
            {
            case AuthType.BasicAuth:
                client.RestClient.Authenticator = new HttpBasicAuthenticator(user.UserData.UserName, user.UserData.Password);
                break;

            case AuthType.TokenAuth:
                client.RestClient.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(user.UserData.Token);
                break;

            case AuthType.SSLAuth:
                client.RestClient.ClientCertificates = new X509CertificateCollection()
                {
                    KubeConfigUtils.GenerateClientCert(user)
                };
                break;

            default:
                break;
            }
            return(new KubernetesClient(new Configuration(client)));
        }