Exemple #1
0
        /// <summary>
        ///     Configure <see cref="KubeClientOptions"/> from the settings specified in the <see cref="K8sConfig"/>.
        /// </summary>
        /// <param name="kubeClientOptions">
        ///
        /// </param>
        /// <param name="kubeContextName">
        ///     The name of the Kubernetes context to use.
        ///
        ///     If not specified, then the current context (as configured) will be used.
        /// </param>
        /// <param name="defaultKubeNamespace">
        ///     The default Kubernetes namespace to use.
        /// </param>
        /// <returns>
        ///     The configured <see cref="KubeClientOptions"/>.
        /// </returns>
        public KubeClientOptions ConfigureKubeClientOptions(KubeClientOptions kubeClientOptions, string kubeContextName = null, string defaultKubeNamespace = null)
        {
            if (kubeClientOptions == null)
            {
                throw new ArgumentNullException(nameof(kubeClientOptions));
            }

            string targetContextName = kubeContextName ?? CurrentContextName;

            if (String.IsNullOrWhiteSpace(targetContextName))
            {
                throw new InvalidOperationException("The kubeContextName parameter was not specified, and the Kubernetes client configuration does not specify a current context.");
            }

            Context targetContext = Contexts.Find(context => context.Name == targetContextName);

            if (targetContext == null)
            {
                throw new InvalidOperationException($"Cannot find a context in the Kubernetes client configuration named '{targetContextName}'.");
            }

            Cluster targetCluster = Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);

            if (targetCluster == null)
            {
                throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");
            }

            UserIdentity targetUser = UserIdentities.Find(user => user.Name == targetContext.Config.UserName);

            if (targetUser == null)
            {
                throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");
            }

            kubeClientOptions.ApiEndPoint       = new Uri(targetCluster.Config.Server);
            kubeClientOptions.KubeNamespace     = defaultKubeNamespace;
            kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
            kubeClientOptions.AllowInsecure     = targetCluster.Config.AllowInsecure;
            kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();
            kubeClientOptions.AccessToken = targetUser.Config.GetRawToken();

            return(kubeClientOptions);
        }
Exemple #2
0
        /// <summary>
        ///     Configure <see cref="KubeClientOptions"/> from the settings specified in the <see cref="K8sConfig"/>.
        /// </summary>
        /// <param name="kubeClientOptions">
        ///
        /// </param>
        /// <param name="kubeContextName">
        ///     The name of the Kubernetes context to use.
        ///
        ///     If not specified, then the current context (as configured) will be used.
        /// </param>
        /// <param name="defaultKubeNamespace">
        ///     The default Kubernetes namespace to use.
        /// </param>
        /// <returns>
        ///     The configured <see cref="KubeClientOptions"/>.
        /// </returns>
        public KubeClientOptions ConfigureKubeClientOptions(KubeClientOptions kubeClientOptions, string kubeContextName = null, string defaultKubeNamespace = null)
        {
            if (kubeClientOptions == null)
            {
                throw new ArgumentNullException(nameof(kubeClientOptions));
            }

            string targetContextName = kubeContextName ?? CurrentContextName;

            if (String.IsNullOrWhiteSpace(targetContextName))
            {
                throw new InvalidOperationException("The kubeContextName parameter was not specified, and the Kubernetes client configuration does not specify a current context.");
            }

            Context targetContext = Contexts.Find(context => context.Name == targetContextName);

            if (targetContext == null)
            {
                throw new InvalidOperationException($"Cannot find a context in the Kubernetes client configuration named '{targetContextName}'.");
            }

            Cluster targetCluster = Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);

            if (targetCluster == null)
            {
                throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");
            }

            UserIdentity targetUser = UserIdentities.Find(user => user.Name == targetContext.Config.UserName);

            if (targetUser == null)
            {
                throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");
            }

            kubeClientOptions.ApiEndPoint       = new Uri(targetCluster.Config.Server);
            kubeClientOptions.KubeNamespace     = defaultKubeNamespace;
            kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
            kubeClientOptions.AllowInsecure     = targetCluster.Config.AllowInsecure;
            kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();

            // Mixed authentication types are not supported.
            if (kubeClientOptions.ClientCertificate == null)
            {
                string accessToken = targetUser.Config.GetRawToken();
                if (!String.IsNullOrWhiteSpace(accessToken))
                {
                    kubeClientOptions.AccessToken  = accessToken;
                    kubeClientOptions.AuthStrategy = KubeAuthStrategy.BearerToken;
                }
                else
                {
                    kubeClientOptions.AuthStrategy = KubeAuthStrategy.None;
                }

                AuthProviderConfig authProvider = targetUser.Config.AuthProvider;
                if (authProvider != null)
                {
                    kubeClientOptions.AuthStrategy = KubeAuthStrategy.BearerTokenProvider;

                    if (authProvider.Config.TryGetValue("cmd-path", out object accessTokenCommand))
                    {
                        kubeClientOptions.AccessTokenCommand = (string)accessTokenCommand;
                    }

                    if (authProvider.Config.TryGetValue("cmd-args", out object accessTokenCommandArguments))
                    {
                        kubeClientOptions.AccessTokenCommandArguments = (string)accessTokenCommandArguments;
                    }

                    if (authProvider.Config.TryGetValue("token-key", out object accessTokenSelector))
                    {
                        kubeClientOptions.AccessTokenSelector = (string)accessTokenSelector;
                    }

                    if (authProvider.Config.TryGetValue("expiry-key", out object accessTokenExpirySelector))
                    {
                        kubeClientOptions.AccessTokenExpirySelector = (string)accessTokenExpirySelector;
                    }

                    if (authProvider.Config.TryGetValue("access-token", out object initialAccessToken))
                    {
                        kubeClientOptions.InitialAccessToken = (string)initialAccessToken;
                    }

                    if (authProvider.Config.TryGetValue("expiry", out object initialTokenExpiry))
                    {
                        kubeClientOptions.InitialTokenExpiryUtc = DateTime.Parse((string)initialTokenExpiry,
                                                                                 provider: CultureInfo.InvariantCulture,
                                                                                 styles: DateTimeStyles.AssumeUniversal
                                                                                 );
                    }
                }
            }
            else
            {
                kubeClientOptions.AuthStrategy = KubeAuthStrategy.ClientCertificate;
            }

            return(kubeClientOptions);
        }
        //Should not be called from more instances on one database -> concurrency issues
        //Does not contain modification of existing data or adding new records
        private void EnsureDatabaseSeeded()
        {
            _log.Info("Seed data into tha database started.");

            InsertOrUpdateSeedData(MiddlewareContextSettings.Languages);
            InsertOrUpdateSeedData(MiddlewareContextSettings.AdaptorUsers);
            InsertOrUpdateSeedData(MiddlewareContextSettings.AdaptorUserRoles);
            InsertOrUpdateSeedData(MiddlewareContextSettings.AdaptorUserGroups);
            InsertOrUpdateSeedData(MiddlewareContextSettings.AdaptorUserUserGroups, false);
            InsertOrUpdateSeedData(GetAllUserRoles(MiddlewareContextSettings.AdaptorUserUserRoles), false);

            InsertOrUpdateSeedData(MiddlewareContextSettings.Clusters?.Select(c => new Cluster
            {
                AuthenticationCredentials = c.AuthenticationCredentials,
                ConnectionProtocol        = c.ConnectionProtocol,
                Description    = c.Description,
                Id             = c.Id,
                MasterNodeName = c.MasterNodeName,
                DomainName     = c.DomainName,
                Port           = c.Port,
                Name           = c.Name,
                NodeTypes      = c.NodeTypes,
                SchedulerType  = c.SchedulerType,
                LocalBasepath  = c.LocalBasepath,
                TimeZone       = c.TimeZone,
                UpdateJobStateByServiceAccount = c.UpdateJobStateByServiceAccount
            }));

            InsertOrUpdateSeedData(MiddlewareContextSettings.ClusterAuthenticationCredentials?.Select(cc => new ClusterAuthenticationCredentials
            {
                Id                 = cc.Id,
                Username           = cc.Username,
                Password           = cc.Password,
                PrivateKeyFile     = cc.PrivateKeyFile,
                PrivateKeyPassword = cc.PrivateKeyPassword,
                ClusterId          = cc.ClusterId,
                Cluster            = cc.Cluster,
                AuthenticationType = GetCredentialsAuthenticationType(cc)
            }));

            InsertOrUpdateSeedData(MiddlewareContextSettings.FileTransferMethods);
            InsertOrUpdateSeedData(MiddlewareContextSettings.JobTemplates);
            InsertOrUpdateSeedData(MiddlewareContextSettings.TaskTemplates);
            InsertOrUpdateSeedData(MiddlewareContextSettings.ClusterNodeTypes);
            InsertOrUpdateSeedData(MiddlewareContextSettings.CommandTemplates);
            InsertOrUpdateSeedData(MiddlewareContextSettings.CommandTemplateParameters);
            InsertOrUpdateSeedData(MiddlewareContextSettings.PropertyChangeSpecifications);

            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackInstances);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackDomains);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackProjects);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackProjectDomains);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackAuthenticationCredentials);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackAuthenticationCredentialDomains, false);
            InsertOrUpdateSeedData(MiddlewareContextSettings.OpenStackAuthenticationCredentialProjectDomains, false);

            //Update Cluster foreign keys which could not be added before
            MiddlewareContextSettings.Clusters?.ForEach(c => Clusters.Find(c.Id).ServiceAccountCredentialsId = c.ServiceAccountCredentialsId);
            SaveChanges();

            var entries = ChangeTracker.Entries();

            //Prevents duplicit entries in memory when items updated
            entries.ToList().ForEach(e => e.State = EntityState.Detached);

            SaveChanges();
            _log.Info("Seed data into the database completed.");
        }