Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check CoreDNS #120

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: update after dependent vendor PR merged.


require (
github.com/longhorn/go-common-libs v0.0.0-20241109101504-614ae31d2212
github.com/longhorn/longhorn-manager v1.7.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions pkg/consts/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const (
AppNamePreflightChecker = "longhorn-preflight-checker"
AppNamePreflightContainerOptimizedOS = "longhorn-gke-cos-node-agent"
AppNamePreflightInstaller = "longhorn-preflight-installer"

AppNameCoreDNS = "coredns"
)

type DependencyModuleType int
Expand Down
55 changes: 45 additions & 10 deletions pkg/local/preflight/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
c3y1huang marked this conversation as resolved.
Show resolved Hide resolved

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))
}
2 changes: 1 addition & 1 deletion pkg/remote/preflight/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/longhorn/go-common-libs/kubernetes/deployment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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