Skip to content

Commit

Permalink
refactor: public some private func
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Sep 12, 2023
1 parent f1a4f70 commit fd1825e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
80 changes: 40 additions & 40 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@ func StsConditions(u *unstructured.Unstructured) (*Result, error) {

if specReplicas > statusReplicas {
message := fmt.Sprintf("Replicas: %d/%d", statusReplicas, specReplicas)
return newInProgressStatus(tooFewReplicas, message), nil
return NewInProgressStatus(tooFewReplicas, message), nil
}

if specReplicas > readyReplicas {
message := fmt.Sprintf("Ready: %d/%d", readyReplicas, specReplicas)
return newInProgressStatus(tooFewReady, message), nil
return NewInProgressStatus(tooFewReady, message), nil
}

if statusReplicas > specReplicas {
message := fmt.Sprintf("Pending termination: %d", statusReplicas-specReplicas)
return newInProgressStatus(extraPods, message), nil
return NewInProgressStatus(extraPods, message), nil
}

// https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#partitions
if partition != -1 {
if updatedReplicas < (specReplicas - partition) {
message := fmt.Sprintf("updated: %d/%d", updatedReplicas, specReplicas-partition)
return newInProgressStatus("PartitionRollout", message), nil
return NewInProgressStatus("PartitionRollout", message), nil
}
// Partition case All ok
return &Result{
Expand All @@ -129,15 +129,15 @@ func StsConditions(u *unstructured.Unstructured) (*Result, error) {

if specReplicas > currentReplicas {
message := fmt.Sprintf("current: %d/%d", currentReplicas, specReplicas)
return newInProgressStatus("LessCurrent", message), nil
return NewInProgressStatus("LessCurrent", message), nil
}

// Revision
currentRevision := GetStringField(obj, ".status.currentRevision", "")
updatedRevision := GetStringField(obj, ".status.updateRevision", "")
if currentRevision != updatedRevision {
message := "Waiting for updated revision to match current"
return newInProgressStatus("RevisionMismatch", message), nil
return NewInProgressStatus("RevisionMismatch", message), nil
}

// All ok
Expand Down Expand Up @@ -206,37 +206,37 @@ func DeploymentConditions(u *unstructured.Unstructured) (*Result, error) {

if specReplicas > statusReplicas {
message := fmt.Sprintf("Replicas: %d/%d", statusReplicas, specReplicas)
return newInProgressStatus(tooFewReplicas, message), nil
return NewInProgressStatus(tooFewReplicas, message), nil
}

if specReplicas > updatedReplicas {
message := fmt.Sprintf("Updated: %d/%d", updatedReplicas, specReplicas)
return newInProgressStatus(tooFewUpdated, message), nil
return NewInProgressStatus(tooFewUpdated, message), nil
}

if statusReplicas > specReplicas {
message := fmt.Sprintf("Pending termination: %d", statusReplicas-specReplicas)
return newInProgressStatus(extraPods, message), nil
return NewInProgressStatus(extraPods, message), nil
}

if updatedReplicas > availableReplicas {
message := fmt.Sprintf("Available: %d/%d", availableReplicas, updatedReplicas)
return newInProgressStatus(tooFewAvailable, message), nil
return NewInProgressStatus(tooFewAvailable, message), nil
}

if specReplicas > readyReplicas {
message := fmt.Sprintf("Ready: %d/%d", readyReplicas, specReplicas)
return newInProgressStatus(tooFewReady, message), nil
return NewInProgressStatus(tooFewReady, message), nil
}

// check conditions
if !progressing {
message := "ReplicaSet not Available"
return newInProgressStatus("ReplicaSetNotAvailable", message), nil
return NewInProgressStatus("ReplicaSetNotAvailable", message), nil
}
if !available {
message := "Deployment not Available"
return newInProgressStatus("DeploymentNotAvailable", message), nil
return NewInProgressStatus("DeploymentNotAvailable", message), nil
}
// All ok
return &Result{
Expand All @@ -260,7 +260,7 @@ func ReplicasetConditions(u *unstructured.Unstructured) (*Result, error) {
// https://github.com/kubernetes/kubernetes/blob/a3ccea9d8743f2ff82e41b6c2af6dc2c41dc7b10/pkg/controller/replicaset/replica_set_utils.go
if c.Type == "ReplicaFailure" && c.Status == corev1.ConditionTrue {
message := "Replica Failure condition. Check Pods"
return newInProgressStatus("ReplicaFailure", message), nil
return NewInProgressStatus("ReplicaFailure", message), nil
}
}

Expand All @@ -273,22 +273,22 @@ func ReplicasetConditions(u *unstructured.Unstructured) (*Result, error) {

if specReplicas > fullyLabelledReplicas {
message := fmt.Sprintf("Labelled: %d/%d", fullyLabelledReplicas, specReplicas)
return newInProgressStatus("LessLabelled", message), nil
return NewInProgressStatus("LessLabelled", message), nil
}

if specReplicas > availableReplicas {
message := fmt.Sprintf("Available: %d/%d", availableReplicas, specReplicas)
return newInProgressStatus(tooFewAvailable, message), nil
return NewInProgressStatus(tooFewAvailable, message), nil
}

if specReplicas > readyReplicas {
message := fmt.Sprintf("Ready: %d/%d", readyReplicas, specReplicas)
return newInProgressStatus(tooFewReady, message), nil
return NewInProgressStatus(tooFewReady, message), nil
}

if statusReplicas > specReplicas {
message := fmt.Sprintf("Pending termination: %d", statusReplicas-specReplicas)
return newInProgressStatus(extraPods, message), nil
return NewInProgressStatus(extraPods, message), nil
}
// All ok
return &Result{
Expand Down Expand Up @@ -321,27 +321,27 @@ func DaemonsetConditions(u *unstructured.Unstructured) (*Result, error) {

if desiredNumberScheduled == -1 {
message := "Missing .status.desiredNumberScheduled"
return newInProgressStatus("NoDesiredNumber", message), nil
return NewInProgressStatus("NoDesiredNumber", message), nil
}

if desiredNumberScheduled > currentNumberScheduled {
message := fmt.Sprintf("Current: %d/%d", currentNumberScheduled, desiredNumberScheduled)
return newInProgressStatus("LessCurrent", message), nil
return NewInProgressStatus("LessCurrent", message), nil
}

if desiredNumberScheduled > updatedNumberScheduled {
message := fmt.Sprintf("Updated: %d/%d", updatedNumberScheduled, desiredNumberScheduled)
return newInProgressStatus(tooFewUpdated, message), nil
return NewInProgressStatus(tooFewUpdated, message), nil
}

if desiredNumberScheduled > numberAvailable {
message := fmt.Sprintf("Available: %d/%d", numberAvailable, desiredNumberScheduled)
return newInProgressStatus(tooFewAvailable, message), nil
return NewInProgressStatus(tooFewAvailable, message), nil
}

if desiredNumberScheduled > numberReady {
message := fmt.Sprintf("Ready: %d/%d", numberReady, desiredNumberScheduled)
return newInProgressStatus(tooFewReady, message), nil
return NewInProgressStatus(tooFewReady, message), nil
}

// All ok
Expand All @@ -364,7 +364,7 @@ func checkGenerationSet(u *unstructured.Unstructured) (*Result, error) {
return &Result{
Status: InProgressStatus,
Message: message,
Conditions: []Condition{newReconcilingCondition("NoGeneration", message)},
Conditions: []Condition{NewReconcilingCondition("NoGeneration", message)},
}, nil
}

Expand All @@ -377,7 +377,7 @@ func checkGenerationSet(u *unstructured.Unstructured) (*Result, error) {
return &Result{
Status: InProgressStatus,
Message: message,
Conditions: []Condition{newReconcilingCondition("NoObservedGeneration", message)},
Conditions: []Condition{NewReconcilingCondition("NoObservedGeneration", message)},
}, nil
}

Expand All @@ -391,7 +391,7 @@ func PvcConditions(u *unstructured.Unstructured) (*Result, error) {
phase := GetStringField(obj, ".status.phase", "unknown")
if phase != "Bound" { // corev1.ClaimBound
message := fmt.Sprintf("PVC is not Bound. phase: %s", phase)
return newInProgressStatus("NotBound", message), nil
return NewInProgressStatus("NotBound", message), nil
}
// All ok
return &Result{
Expand Down Expand Up @@ -424,7 +424,7 @@ func PodConditions(u *unstructured.Unstructured) (*Result, error) {
Conditions: []Condition{},
}, nil
case "Running":
if hasConditionWithStatus(objc.Status.Conditions, "Ready", corev1.ConditionTrue) {
if HasConditionWithStatus(objc.Status.Conditions, "Ready", corev1.ConditionTrue) {
return &Result{
Status: CurrentStatus,
Message: "Pod is Ready",
Expand All @@ -437,27 +437,27 @@ func PodConditions(u *unstructured.Unstructured) (*Result, error) {
return nil, err
}
if isCrashLooping {
return newFailedStatus("ContainerCrashLooping",
return NewFailedStatus("ContainerCrashLooping",
fmt.Sprintf("Containers in CrashLoop state: %s", strings.Join(containerNames, ","))), nil
}

return newInProgressStatus("PodRunningNotReady", "Pod is running but is not Ready"), nil
return NewInProgressStatus("PodRunningNotReady", "Pod is running but is not Ready"), nil
case "Pending":
c, found := getConditionWithStatus(objc.Status.Conditions, "PodScheduled", corev1.ConditionFalse)
c, found := GetConditionWithStatus(objc.Status.Conditions, "PodScheduled", corev1.ConditionFalse)
if found && c.Reason == "Unschedulable" {
if time.Now().Add(-ScheduleWindow).Before(u.GetCreationTimestamp().Time) {
// We give the pod 15 seconds to be scheduled before we report it
// as unschedulable.
return newInProgressStatus("PodNotScheduled", "Pod has not been scheduled"), nil
return NewInProgressStatus("PodNotScheduled", "Pod has not been scheduled"), nil
}
return newFailedStatus("PodUnschedulable", "Pod could not be scheduled"), nil
return NewFailedStatus("PodUnschedulable", "Pod could not be scheduled"), nil
}
return newInProgressStatus("PodPending", "Pod is in the Pending phase"), nil
return NewInProgressStatus("PodPending", "Pod is in the Pending phase"), nil
default:
// If the controller hasn't observed the pod yet, there is no phase. We consider this as it
// still being in progress.
if phase == "" {
return newInProgressStatus("PodNotObserved", "Pod phase not available"), nil
return NewInProgressStatus("PodNotObserved", "Pod phase not available"), nil
}
return nil, fmt.Errorf("unknown phase %s", phase)
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func JobConditions(u *unstructured.Unstructured) (*Result, error) {
}
case "Failed":
if c.Status == corev1.ConditionTrue {
return newFailedStatus("JobFailed",
return NewFailedStatus("JobFailed",
fmt.Sprintf("Job Failed. failed: %d/%d", failed, completions)), nil
}
}
Expand All @@ -565,7 +565,7 @@ func JobConditions(u *unstructured.Unstructured) (*Result, error) {
// replicas
if starttime == "" {
message := "Job not started"
return newInProgressStatus("JobNotStarted", message), nil
return NewInProgressStatus("JobNotStarted", message), nil
}
return &Result{
Status: CurrentStatus,
Expand All @@ -584,7 +584,7 @@ func ServiceConditions(u *unstructured.Unstructured) (*Result, error) {
if specType == "LoadBalancer" {
if specClusterIP == "" {
message := "ClusterIP not set. Service type: LoadBalancer"
return newInProgressStatus("NoIPAssigned", message), nil
return NewInProgressStatus("NoIPAssigned", message), nil
}
}

Expand All @@ -605,11 +605,11 @@ func CRDConditions(u *unstructured.Unstructured) (*Result, error) {

for _, c := range objc.Status.Conditions {
if c.Type == "NamesAccepted" && c.Status == corev1.ConditionFalse {
return newFailedStatus(c.Reason, c.Message), nil
return NewFailedStatus(c.Reason, c.Message), nil
}
if c.Type == "Established" {
if c.Status == corev1.ConditionFalse && c.Reason != "Installing" {
return newFailedStatus(c.Reason, c.Message), nil
return NewFailedStatus(c.Reason, c.Message), nil
}
if c.Status == corev1.ConditionTrue {
return &Result{
Expand All @@ -620,5 +620,5 @@ func CRDConditions(u *unstructured.Unstructured) (*Result, error) {
}
}
}
return newInProgressStatus("Installing", "Install in progress"), nil
return NewInProgressStatus("Installing", "Install in progress"), nil
}
4 changes: 2 additions & 2 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CheckGenericProperties(u *unstructured.Unstructured) (*Result, error) {

for _, cond := range objWithConditions.Status.Conditions {
if cond.Type == string(ConditionReconciling) && cond.Status == corev1.ConditionTrue {
return newInProgressStatus(cond.Reason, cond.Message), nil
return NewInProgressStatus(cond.Reason, cond.Message), nil
}
if cond.Type == string(ConditionStalled) && cond.Status == corev1.ConditionTrue {
return &Result{
Expand Down Expand Up @@ -88,7 +88,7 @@ func checkGeneration(u *unstructured.Unstructured) (*Result, error) {
return &Result{
Status: InProgressStatus,
Message: message,
Conditions: []Condition{newReconcilingCondition("LatestGenerationNotObserved", message)},
Conditions: []Condition{NewReconcilingCondition("LatestGenerationNotObserved", message)},
}, nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ func CheckReadyCondition(u *unstructured.Unstructured) (*Result, error) {
Conditions: []Condition{},
}, nil
case corev1.ConditionFalse:
return newInProgressStatus(cond.Reason, cond.Message), nil
return NewInProgressStatus(cond.Reason, cond.Message), nil
case corev1.ConditionUnknown:
// For now we just treat an unknown condition value as
// InProgress. We should consider if there are better ways
// to handle it.
return newInProgressStatus(cond.Reason, cond.Message), nil
return NewInProgressStatus(cond.Reason, cond.Message), nil
default:
// Do nothing in this case.
}
Expand Down
22 changes: 11 additions & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// newReconcilingCondition creates an reconciling condition with the given
// NewReconcilingCondition creates an reconciling condition with the given
// reason and message.
func newReconcilingCondition(reason, message string) Condition {
func NewReconcilingCondition(reason, message string) Condition {
return Condition{
Type: ConditionReconciling,
Status: corev1.ConditionTrue,
Expand All @@ -19,7 +19,7 @@ func newReconcilingCondition(reason, message string) Condition {
}
}

func newStalledCondition(reason, message string) Condition {
func NewStalledCondition(reason, message string) Condition {
return Condition{
Type: ConditionStalled,
Status: corev1.ConditionTrue,
Expand All @@ -28,21 +28,21 @@ func newStalledCondition(reason, message string) Condition {
}
}

// newInProgressStatus creates a status Result with the InProgress status
// NewInProgressStatus creates a status Result with the InProgress status
// and an InProgress condition.
func newInProgressStatus(reason, message string) *Result {
func NewInProgressStatus(reason, message string) *Result {
return &Result{
Status: InProgressStatus,
Message: message,
Conditions: []Condition{newReconcilingCondition(reason, message)},
Conditions: []Condition{NewReconcilingCondition(reason, message)},
}
}

func newFailedStatus(reason, message string) *Result {
func NewFailedStatus(reason, message string) *Result {
return &Result{
Status: FailedStatus,
Message: message,
Conditions: []Condition{newStalledCondition(reason, message)},
Conditions: []Condition{NewStalledCondition(reason, message)},
}
}

Expand Down Expand Up @@ -82,12 +82,12 @@ func GetObjectWithConditions(in map[string]interface{}) (*ObjWithConditions, err
return out, nil
}

func hasConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) bool {
_, found := getConditionWithStatus(conditions, conditionType, status)
func HasConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) bool {
_, found := GetConditionWithStatus(conditions, conditionType, status)
return found
}

func getConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) (BasicCondition, bool) {
func GetConditionWithStatus(conditions []BasicCondition, conditionType string, status corev1.ConditionStatus) (BasicCondition, bool) {
for _, c := range conditions {
if c.Type == conditionType && c.Status == status {
return c, true
Expand Down

0 comments on commit fd1825e

Please sign in to comment.