From a71f237c25bc3d9fbdec1e218277e77deffbb870 Mon Sep 17 00:00:00 2001 From: lihao <310103270@qq.com> Date: Thu, 5 May 2022 16:03:46 +0800 Subject: [PATCH] =?UTF-8?q?[IMP]=20=E4=BF=AE=E6=94=B9agent=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/command/agent/agent.go | 128 +-------------------------------- pkg/command/agent/agent_env.go | 5 +- 2 files changed, 6 insertions(+), 127 deletions(-) diff --git a/pkg/command/agent/agent.go b/pkg/command/agent/agent.go index f44d4822..49bdc444 100644 --- a/pkg/command/agent/agent.go +++ b/pkg/command/agent/agent.go @@ -7,18 +7,14 @@ import ( helmcommon "github.com/choerodon/choerodon-cluster-agent/pkg/command/helm" "github.com/choerodon/choerodon-cluster-agent/pkg/gitops" "github.com/choerodon/choerodon-cluster-agent/pkg/helm" - "github.com/choerodon/choerodon-cluster-agent/pkg/helm/helm2to3" "github.com/choerodon/choerodon-cluster-agent/pkg/operator" commandutil "github.com/choerodon/choerodon-cluster-agent/pkg/util/command" "github.com/choerodon/choerodon-cluster-agent/pkg/util/controller" "github.com/choerodon/choerodon-cluster-agent/pkg/util/errors" "github.com/golang/glog" - "github.com/open-hand/helm/pkg/release" - appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "math/rand" - "strings" "time" ) @@ -51,11 +47,6 @@ func InitAgent(opts *commandutil.Opts, cmd *model.Packet) ([]*model.Packet, *mod model.CertManagerVersion = agentInitOpts.CertManagerVersion - err = agentConvert(opts, agentInitOpts.AgentName) - if err != nil { - return nil, commandutil.NewResponseError(cmd.Key, model.InitAgentFailed, err) - } - namespaces := opts.Namespaces g := gitops.New(opts.Wg, opts.GitConfig, opts.GitRepos, opts.KubeClient, opts.Cluster, opts.CrChan) @@ -172,137 +163,22 @@ func ReSyncAgent(opts *commandutil.Opts, cmd *model.Packet) ([]*model.Packet, *m // 如果命名空间存在则检查labels,是否设置 "choerodon.io/helm-version":"helm3" // 未设置helm标签,则添加helm标签并调用helm升级函数,将helm实例从helm2升级到helm3 func createNamespace(opts *commandutil.Opts, namespaceName string, releases []string) error { - ns, err := opts.KubeClient.GetKubeClient().CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{}) + _, err := opts.KubeClient.GetKubeClient().CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{}) if err != nil { // 如果命名空间不存在的话,则创建 if errors.IsNotFound(err) { _, err = opts.KubeClient.GetKubeClient().CoreV1().Namespaces().Create(&corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: namespaceName, - Labels: map[string]string{model.HelmVersion: "helm3"}, + Name: namespaceName, }, }) return err } return err } - - labels := ns.Labels - annotations := ns.Annotations - // 如果命名空间存在,则检查labels标签 - if _, ok := labels[model.HelmVersion]; !ok { - return update(opts, releases, namespaceName, labels, annotations) - } - return nil -} - -func update(opts *commandutil.Opts, releases []string, namespaceName string, labels, annotations map[string]string) error { - releaseCount := len(releases) - upgradeCount := 0 - - // 此处不对choerodon命名空间下的实例进行升级处理 - // 安装agent的时候,会直接创建choerodon命名空间而不打上 model.HelmVersion 标签 - // 然后用户直接创建pv,会导致choerodon没有标签也纳入环境管理(如果通过agent安装了prometheus或者cert-manager就会出现问题) - // 所以直接默认choeordon不需要进行helm迁移 - if namespaceName != "choerodon" && releaseCount != 0 { - for i := 0; i < releaseCount; i++ { - getReleaseRequest := &helm.GetReleaseContentRequest{ - ReleaseName: releases[i], - Namespace: namespaceName, - } - - // 查看该实例是否helm3管理,如果是upgradeCount加1,如果不是,进行升级操作然后再加1 - _, err := opts.HelmClient.GetRelease(getReleaseRequest) - if err != nil { - // 实例不存在有可能是实例未升级,尝试升级操作 - if strings.Contains(err.Error(), helm.ErrReleaseNotFound) { - helm2to3.RunConvert(releases[i]) - if opts.ClearHelmHistory { - helm2to3.RunCleanup(releases[i]) - } - upgradeCount++ - } - } else { - // 实例存在表明实例被helm3管理,尝试进行数据清理,然后upgradeCount加1 - if opts.ClearHelmHistory { - helm2to3.RunCleanup(releases[i]) - } - upgradeCount++ - } - } - - if releaseCount != upgradeCount { - return fmt.Errorf("env %s : failed to upgrade helm2 to helm3 ", namespaceName) - } - } - - if labels == nil { - labels = make(map[string]string) - } - - labels[model.HelmVersion] = "helm3" - _, err := opts.KubeClient.GetKubeClient().CoreV1().Namespaces().Update(&corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: namespaceName, - Labels: labels, - Annotations: annotations, - }, - }) - return err -} - -func agentConvert(opts *commandutil.Opts, agentName string) error { - - deployment, err := opts.KubeClient.GetKubeClient().AppsV1().Deployments(model.AgentNamespace).Get(agentName, metav1.GetOptions{}) - if err != nil { - return err - } - labels := deployment.ObjectMeta.GetLabels() - annotations := deployment.ObjectMeta.GetAnnotations() - - // agent实例是否由helm3进行管理的 - if labels[model.HelmVersion] != "helm3" { - releaseRequest := &helm.GetReleaseContentRequest{ - ReleaseName: agentName, - Namespace: model.AgentNamespace, - } - rls, _ := opts.HelmClient.GetRelease(releaseRequest) - - // 实例由helm3管理,更新标签 - if rls != nil { - if rls.Status != release.StatusDeployed.String() { - return fmt.Errorf("agent: %s,status %s", agentName, rls.Status) - } - updateAgentDeploymentLabels(opts, deployment, labels, annotations) - } else { - // 实例由helm2管理,先升级成helm3管理,然后更新标签 - err = helm2to3.RunConvert(agentName) - // 如果从helm2升级到helm3没有问题,就清理helm2的数据并给agent的deployment添加标签 - if err == nil { - if opts.ClearHelmHistory { - helm2to3.RunCleanup(agentName) - } - updateAgentDeploymentLabels(opts, deployment, labels, annotations) - } else { - return err - } - } - } return nil } -func updateAgentDeploymentLabels(opts *commandutil.Opts, deployment *appsv1.Deployment, labels, annotations map[string]string) { - if labels == nil { - labels = make(map[string]string) - } - if annotations == nil { - annotations = make(map[string]string) - } - labels[model.HelmVersion] = "helm3" - deployment.SetLabels(labels) - opts.KubeClient.GetKubeClient().AppsV1().Deployments(model.AgentNamespace).Update(deployment) -} - func getClusterInfo(opts *commandutil.Opts, cmd *model.Packet) ([]*model.Packet, *model.Packet) { listOpts := metav1.ListOptions{} diff --git a/pkg/command/agent/agent_env.go b/pkg/command/agent/agent_env.go index 706238f0..5e0b1fe1 100644 --- a/pkg/command/agent/agent_env.go +++ b/pkg/command/agent/agent_env.go @@ -20,7 +20,10 @@ func AddEnv(opts *commandutil.Opts, cmd *model.Packet) ([]*model.Packet, *model. return nil, commandutil.NewResponseError(cmd.Key, model.EnvCreateFailed, err) } - skipCheckNamespace := os.Getenv("SKIP_CHECK_EXIST_NAMESPACE") == "True" + var skipCheckNamespace bool + if model.RestrictedModel { + skipCheckNamespace = true + } if err = opts.KubeClient.GetNamespace(agentInitOpts.Envs[0].Namespace); err == nil && agentInitOpts.Envs[0].Namespace != "choerodon" && agentInitOpts.Envs[0].Namespace != model.AgentNamespace && !skipCheckNamespace { return nil, commandutil.NewResponseError(cmd.Key, model.EnvCreateFailed, fmt.Errorf("env %s already exist", agentInitOpts.Envs[0].Namespace))