Skip to content

Commit

Permalink
Merge pull request #94 from sanchezl/statusgeneration
Browse files Browse the repository at this point in the history
add progressing status unit test and updated messages
  • Loading branch information
openshift-merge-robot committed Dec 18, 2018
2 parents 45e752c + d46671f commit 053a0c4
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/operator/workload_controller_openshiftapiserver_v311_00.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operator

import (
"fmt"
"strings"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -111,8 +112,15 @@ func syncOpenShiftAPIServer_v311_00_to_latest(c OpenShiftAPIServerOperator, orig
}

// If the daemonset is up to date and the operatorConfig are up to date, then we are no longer progressing
if actualDaemonSet.ObjectMeta.Generation == actualDaemonSet.Status.ObservedGeneration &&
operatorConfig.ObjectMeta.Generation == operatorConfig.Status.ObservedGeneration {
var progressingMessages []string
if actualDaemonSet.ObjectMeta.Generation != actualDaemonSet.Status.ObservedGeneration {
progressingMessages = append(progressingMessages, fmt.Sprintf("daemonset/apiserver.openshift-operator: observed generation is %d, desired generation is %d.", actualDaemonSet.Status.ObservedGeneration, actualDaemonSet.ObjectMeta.Generation))
}
if operatorConfig.ObjectMeta.Generation != operatorConfig.Status.ObservedGeneration {
progressingMessages = append(progressingMessages, fmt.Sprintf("openshiftapiserveroperatorconfigs/instance: observed generation is %d, desired generation is %d.", operatorConfig.Status.ObservedGeneration, operatorConfig.ObjectMeta.Generation))
}

if len(progressingMessages) == 0 {
v1helpers.SetOperatorCondition(&operatorConfig.Status.Conditions, operatorv1.OperatorCondition{
Type: operatorv1.OperatorStatusTypeProgressing,
Status: operatorv1.ConditionFalse,
Expand All @@ -122,9 +130,10 @@ func syncOpenShiftAPIServer_v311_00_to_latest(c OpenShiftAPIServerOperator, orig
Type: operatorv1.OperatorStatusTypeProgressing,
Status: operatorv1.ConditionTrue,
Reason: "DesiredStateNotYetAchieved",
Message: fmt.Sprintf("Generation: expected: %v, actual: %v", operatorConfig.Status.ObservedGeneration, actualDaemonSet.ObjectMeta.Generation),
Message: strings.Join(progressingMessages, "\n"),
})
}

// TODO this is changing too early and it was before too.
operatorConfig.Status.ObservedGeneration = operatorConfig.ObjectMeta.Generation
resourcemerge.SetDaemonSetGeneration(&operatorConfig.Status.Generations, actualDaemonSet)
Expand Down
156 changes: 156 additions & 0 deletions pkg/operator/workload_controller_openshiftapiserver_v311_00_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package operator

import (
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

operatorv1 "github.com/openshift/api/operator/v1"
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
openshiftapiserveroperator "github.com/openshift/cluster-openshift-apiserver-operator/pkg/apis/openshiftapiserver/v1alpha1"
operatorfake "github.com/openshift/cluster-openshift-apiserver-operator/pkg/generated/clientset/versioned/fake"
"github.com/openshift/library-go/pkg/operator/events"
operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers"
)

func TestSyncOpenShiftAPIServer_v311_00_to_latestProgressingCondition(t *testing.T) {

testCases := []struct {
name string
daemonSetGeneration int64
daemonSetObservedGeneration int64
configGeneration int64
configObservedGeneration int64
expectedStatus operatorv1.ConditionStatus
expectedMessage string
}{
{
name: "HappyPath",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 100,
configGeneration: 100,
configObservedGeneration: 100,
expectedStatus: operatorv1.ConditionFalse,
},
{
name: "DaemonSetObservedAhead",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 101,
configGeneration: 100,
configObservedGeneration: 100,
expectedStatus: operatorv1.ConditionTrue,
expectedMessage: "daemonset/apiserver.openshift-operator: observed generation is 101, desired generation is 100.",
},
{
name: "DaemonSetObservedBehind",
daemonSetGeneration: 101,
daemonSetObservedGeneration: 100,
configGeneration: 100,
configObservedGeneration: 100,
expectedStatus: operatorv1.ConditionTrue,
expectedMessage: "daemonset/apiserver.openshift-operator: observed generation is 100, desired generation is 101.",
},
{
name: "ConfigObservedAhead",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 100,
configGeneration: 100,
configObservedGeneration: 101,
expectedStatus: operatorv1.ConditionTrue,
expectedMessage: "openshiftapiserveroperatorconfigs/instance: observed generation is 101, desired generation is 100.",
},
{
name: "ConfigObservedBehind",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 100,
configGeneration: 101,
configObservedGeneration: 100,
expectedStatus: operatorv1.ConditionTrue,
expectedMessage: "openshiftapiserveroperatorconfigs/instance: observed generation is 100, desired generation is 101.",
},
{
name: "MultipleObservedAhead",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 101,
configGeneration: 100,
configObservedGeneration: 101,
expectedStatus: operatorv1.ConditionTrue,
expectedMessage: "daemonset/apiserver.openshift-operator: observed generation is 101, desired generation is 100.\nopenshiftapiserveroperatorconfigs/instance: observed generation is 101, desired generation is 100.",
},
{
name: "ConfigAndDaemonSetGenerationMismatch",
daemonSetGeneration: 100,
daemonSetObservedGeneration: 100,
configGeneration: 101,
configObservedGeneration: 101,
expectedStatus: operatorv1.ConditionFalse,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

kubeClient := fake.NewSimpleClientset(
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "serving-cert", Namespace: "openshift-apiserver"}},
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "etcd-client", Namespace: "kube-system"}},
&appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver",
Namespace: "openshift-apiserver",
Generation: tc.daemonSetGeneration,
},
Status: appsv1.DaemonSetStatus{
NumberAvailable: 100,
ObservedGeneration: tc.daemonSetObservedGeneration,
},
})

operatorConfig := &openshiftapiserveroperator.OpenShiftAPIServerOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "instance",
Generation: tc.configGeneration,
},
Spec: openshiftapiserveroperator.OpenShiftAPIServerOperatorConfigSpec{
OperatorSpec: operatorv1.OperatorSpec{},
},
Status: openshiftapiserveroperator.OpenShiftAPIServerOperatorConfigStatus{
OperatorStatus: operatorv1.OperatorStatus{
ObservedGeneration: tc.configObservedGeneration,
},
},
}
apiServiceOperatorClient := operatorfake.NewSimpleClientset(operatorConfig)
openshiftConfigClient := configfake.NewSimpleClientset()

operator := OpenShiftAPIServerOperator{
kubeClient: kubeClient,
eventRecorder: events.NewInMemoryRecorder(""),
operatorConfigClient: apiServiceOperatorClient.Openshiftapiserver(),
openshiftConfigClient: openshiftConfigClient.ConfigV1(),
}

syncOpenShiftAPIServer_v311_00_to_latest(operator, operatorConfig)

result, err := apiServiceOperatorClient.Openshiftapiserver().OpenShiftAPIServerOperatorConfigs().Get("instance", metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}

condition := operatorv1helpers.FindOperatorCondition(result.Status.Conditions, operatorv1.OperatorStatusTypeProgressing)
if condition == nil {
t.Fatalf("No %v condition found.", operatorv1.OperatorStatusTypeProgressing)
}
if condition.Status != tc.expectedStatus {
t.Errorf("expected status == %v, actual status == %v", tc.expectedStatus, condition.Status)
}
if condition.Message != tc.expectedMessage {
t.Errorf("expected message:\n%v\nactual message:\n%v", tc.expectedMessage, condition.Message)
}

})
}

}

0 comments on commit 053a0c4

Please sign in to comment.