From df942d4de6ab6c0738cd5a9e6e1103218486d877 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Thu, 14 Nov 2024 09:36:42 +0800 Subject: [PATCH 1/4] chore: update vendor longhorn/longhorn-9752 Signed-off-by: Chin-Ya Huang --- go.mod | 2 ++ go.sum | 4 ++-- .../go-common-libs/kubernetes/deployment.go | 23 +++++++++++++++++++ vendor/modules.txt | 3 ++- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 vendor/github.com/longhorn/go-common-libs/kubernetes/deployment.go diff --git a/go.mod b/go.mod index 991c2fbf..678b50ab 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.23 toolchain go1.23.3 +replace github.com/longhorn/go-common-libs => github.com/c3y1huang/go-common-libs v0.0.0-20241114031240-e1117130cd3b + require ( github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212 github.com/longhorn/longhorn-manager v1.7.2 diff --git a/go.sum b/go.sum index d3576e46..171f5f70 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/c3y1huang/go-common-libs v0.0.0-20241114031240-e1117130cd3b h1:mXbX1SYCZnxrIO/s5gfKSqMMbJMHnwlQwNDUKCyBnTM= +github.com/c3y1huang/go-common-libs v0.0.0-20241114031240-e1117130cd3b/go.mod h1:MXrEgkGm+fI2dHRUuBCiKOG+UHySQXYC8ecjTFBvvho= github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK84APFuMvxqsk3tEIaKH/z4Rpu3g= github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE= github.com/chai2010/gettext-go v1.0.2 h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNSjIRk= @@ -102,8 +104,6 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY= github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= -github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212 h1:wmbGfP3yp4B35Nbd9mNwtOEOih4QrWvOXG3jXkisCgM= -github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212/go.mod h1:MXrEgkGm+fI2dHRUuBCiKOG+UHySQXYC8ecjTFBvvho= github.com/longhorn/longhorn-manager v1.7.2 h1:hf0phTtk8Odq+zoVYO5JuHbKJOi4nK+iugUJ1FQw56M= github.com/longhorn/longhorn-manager v1.7.2/go.mod h1:QvOuwQP3TqICQHyJcPynjtAhF3TMCFeygVIpf9q7/cI= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= diff --git a/vendor/github.com/longhorn/go-common-libs/kubernetes/deployment.go b/vendor/github.com/longhorn/go-common-libs/kubernetes/deployment.go new file mode 100644 index 00000000..d879832b --- /dev/null +++ b/vendor/github.com/longhorn/go-common-libs/kubernetes/deployment.go @@ -0,0 +1,23 @@ +package kubernetes + +import ( + "context" + + "github.com/sirupsen/logrus" + + appsv1 "k8s.io/api/apps/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubeclient "k8s.io/client-go/kubernetes" +) + +// GetDeployment returns the Deployment with the given name in the given namespace. +func GetDeployment(kubeClient kubeclient.Interface, namespace, name string) (*appsv1.Deployment, error) { + log := logrus.WithFields(logrus.Fields{ + "kind": "Deployment", + "namespace": namespace, + "name": name, + }) + log.Trace("Getting resource") + + return kubeClient.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{}) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 6fcda838..51307019 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -133,7 +133,7 @@ github.com/json-iterator/go # github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de ## explicit github.com/liggitt/tabwriter -# github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212 +# github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212 => github.com/c3y1huang/go-common-libs v0.0.0-20241114031240-e1117130cd3b ## explicit; go 1.22.7 github.com/longhorn/go-common-libs/exec github.com/longhorn/go-common-libs/io @@ -928,3 +928,4 @@ sigs.k8s.io/structured-merge-diff/v4/value sigs.k8s.io/yaml sigs.k8s.io/yaml/goyaml.v2 sigs.k8s.io/yaml/goyaml.v3 +# github.com/longhorn/go-common-libs => github.com/c3y1huang/go-common-libs v0.0.0-20241114031240-e1117130cd3b From 412957cfa4f2fb4ce16880a9d6972905f7cbb686 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Thu, 14 Nov 2024 10:00:02 +0800 Subject: [PATCH 2/4] feat(checker): check CoreDNS has more than 2 replicas longhorn/longhorn-9752 Signed-off-by: Chin-Ya Huang --- pkg/consts/preflight.go | 2 ++ pkg/local/preflight/checker.go | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pkg/consts/preflight.go b/pkg/consts/preflight.go index 812e2afc..5f26cd65 100644 --- a/pkg/consts/preflight.go +++ b/pkg/consts/preflight.go @@ -4,6 +4,8 @@ const ( AppNamePreflightChecker = "longhorn-preflight-checker" AppNamePreflightContainerOptimizedOS = "longhorn-gke-cos-node-agent" AppNamePreflightInstaller = "longhorn-preflight-installer" + + AppNameCoreDNS = "coredns" ) type DependencyModuleType int diff --git a/pkg/local/preflight/checker.go b/pkg/local/preflight/checker.go index 657d5393..d62177c6 100644 --- a/pkg/local/preflight/checker.go +++ b/pkg/local/preflight/checker.go @@ -177,6 +177,8 @@ func (local *Checker) Init() error { // Run executes the preflight checks. func (local *Checker) Run() error { + local.checkCoreDNS() + switch local.osRelease { case fmt.Sprint(consts.OperatingSystemContainerOptimizedOS): logrus.Infof("Checking preflight for %v", consts.OperatingSystemContainerOptimizedOS) @@ -466,3 +468,36 @@ func (local *Checker) checkNFSv4Support() error { local.collection.Log.Error = append(local.collection.Log.Error, "NFS4 is not supported") return nil } + +// checkCoreDNS checks if the CoreDNS deployment in the Kubernetes cluster +// has multiple replicas and logs warnings if it does not. +// +// It retrieves the CoreDNS deployment from the kubeClient and checks the +// number of replicas specified in the deployment spec. If the number of +// replicas is less than 2, it logs a warning indicating that CoreDNS is +// not set to run in multiple replicas. Additionally, it checks the number +// of ready replicas in the deployment status and logs a warning if there +// are fewer than 2 ready replicas. +// +// https://github.com/longhorn/longhorn/issues/9752 +func (local *Checker) checkCoreDNS() { + logrus.Info("Checking if CoreDNS has multiple replicas") + + deployment, err := commonkube.GetDeployment(local.kubeClient, metav1.NamespaceSystem, consts.AppNameCoreDNS) + if err != nil { + local.collection.Log.Error = append(local.collection.Log.Error, fmt.Sprintf("failed to check CoreDNS: %v", err)) + return + } + + if deployment.Spec.Replicas == nil || *deployment.Spec.Replicas < 2 { + local.collection.Log.Warn = append(local.collection.Log.Warn, "CoreDNS is set with fewer than 2 replicas; consider increasing replica count for high availability") + return + } + + if deployment.Status.ReadyReplicas < 2 { + local.collection.Log.Warn = append(local.collection.Log.Warn, "CoreDNS has fewer than 2 ready replicas; some replicas may not be running or ready") + return + } + + local.collection.Log.Info = append(local.collection.Log.Info, fmt.Sprintf("CoreDNS is set with %d replicas and %d ready replicas", *deployment.Spec.Replicas, deployment.Status.ReadyReplicas)) +} From 15ee7efd5be4afe15e15d8ad353d80bed015d61e Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Thu, 14 Nov 2024 10:16:30 +0800 Subject: [PATCH 3/4] refactor(checker): set kubeclient during initialization Signed-off-by: Chin-Ya Huang --- pkg/local/preflight/checker.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/local/preflight/checker.go b/pkg/local/preflight/checker.go index d62177c6..9102ad8b 100644 --- a/pkg/local/preflight/checker.go +++ b/pkg/local/preflight/checker.go @@ -53,6 +53,16 @@ type Checker struct { func (local *Checker) Init() error { local.collection.Log = &types.LogCollection{} + config, err := commonkube.GetInClusterConfig() + if err != nil { + return errors.Wrap(err, "failed to get client config") + } + + local.kubeClient, err = kubeclient.NewForConfig(config) + if err != nil { + return errors.Wrap(err, "failed to get Kubernetes clientset") + } + osRelease, err := utils.GetOSRelease() if err != nil { return errors.Wrap(err, "failed to get OS release") @@ -61,16 +71,6 @@ func (local *Checker) Init() error { local.logger = logrus.WithField("os", local.osRelease) if local.osRelease == fmt.Sprint(consts.OperatingSystemContainerOptimizedOS) { - config, err := commonkube.GetInClusterConfig() - if err != nil { - return errors.Wrap(err, "failed to get client config") - } - - local.kubeClient, err = kubeclient.NewForConfig(config) - if err != nil { - return errors.Wrap(err, "failed to get Kubernetes clientset") - } - return nil } From af34ac2ec6d59e47c78749213752ca67074c6259 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Thu, 14 Nov 2024 10:32:10 +0800 Subject: [PATCH 4/4] feat(checker): add deployments to clusterrole longhorn/longhorn-9752 Signed-off-by: Chin-Ya Huang --- pkg/remote/preflight/checker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/remote/preflight/checker.go b/pkg/remote/preflight/checker.go index 490ed67b..48c11169 100644 --- a/pkg/remote/preflight/checker.go +++ b/pkg/remote/preflight/checker.go @@ -165,7 +165,7 @@ func (remote *Checker) newClusterRole() *rbacv1.ClusterRole { Rules: []rbacv1.PolicyRule{ { APIGroups: []string{"apps"}, - Resources: []string{"daemonsets"}, + Resources: []string{"daemonsets", "deployments"}, Verbs: []string{"get"}, }, },