Skip to content

Commit

Permalink
feat(checker): check CoreDNS has more than 2 replicas
Browse files Browse the repository at this point in the history
longhorn/longhorn-9752

Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
  • Loading branch information
c3y1huang committed Nov 14, 2024
1 parent df942d4 commit af43940
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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
34 changes: 34 additions & 0 deletions pkg/local/preflight/checker.go
Original file line number Diff line number Diff line change
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,35 @@ 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: %w", 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")
}

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))
}

0 comments on commit af43940

Please sign in to comment.