Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Updated tests and removed last applied annotation
Browse files Browse the repository at this point in the history
Signed-off-by: jaellio <jaellio@microsoft.com>
  • Loading branch information
jaellio committed May 20, 2022
1 parent c232b56 commit 95cc59a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 85 deletions.
19 changes: 5 additions & 14 deletions cmd/osm-bootstrap/osm-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,35 +371,26 @@ func (b *bootstrap) ensureMeshRootCertificate() error {
FieldSelector: "status.state=complete,status.rotationStage=issuing",
}
meshRootCertificateList, err := b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).List(context.TODO(), listOptions)
if err != nil {
return err
}

if len(meshRootCertificateList.Items) == 0 {
// create a MeshRootCertificate since none were found in the complete state and issuing rotationStage
return b.createMeshRootCertificate()
}
if err != nil {
return err
}

meshRootCertificate := meshRootCertificateList.Items[0]
if _, exists := meshRootCertificate.Annotations[corev1.LastAppliedConfigAnnotation]; !exists {
// MeshRootCertificate was found, but may not have the last applied annotation.
if err := util.CreateApplyAnnotation(&meshRootCertificate, unstructured.UnstructuredJSONScheme); err != nil {
return err
}
if _, err := b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).Update(context.TODO(), &meshRootCertificate, metav1.UpdateOptions{}); err != nil {
return err
}
}
return nil
}

func (b *bootstrap) createMeshRootCertificate() error {
// find preset config map to build the MeshRootCertificate from
presetMeshRootCertificate, err := b.kubeClient.CoreV1().ConfigMaps(b.namespace).Get(context.TODO(), presetMeshRootCertificateName, metav1.GetOptions{})
// If the preset MeshRootCertificate could not be loaded return the error
if err != nil {
return err
}
_, err = b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).Get(context.TODO(), "osm-mesh-root-certificate", metav1.GetOptions{})
log.Error().Msgf("HELLLOOO%v", err)

// Create a MeshRootCertificate
defaultMeshConfig, err := buildMeshRootCertificate(presetMeshRootCertificate)
Expand Down
86 changes: 15 additions & 71 deletions cmd/osm-bootstrap/osm-bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
fakeKube "k8s.io/client-go/kubernetes/fake"

configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
"github.com/openservicemesh/osm/pkg/constants"
configClientset "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
fakeConfig "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned/fake"
)
Expand Down Expand Up @@ -96,23 +97,8 @@ var testMeshRootCertificate *configv1alpha2.MeshRootCertificate = &configv1alpha
},
Spec: configv1alpha2.MeshRootCertificateSpec{},
Status: configv1alpha2.MeshRootCertificateStatus{
State: "complete",
RotationStage: "issuing",
},
}

var testMeshRootCertificateWithLastAppliedAnnotation *configv1alpha2.MeshRootCertificate = &configv1alpha2.MeshRootCertificate{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: meshRootCertificateName,
Annotations: map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": `{"metadata":{"name":"osm-mesh-root-certificate","namespace":"test-namespace","creationTimestamp":null},"spec":{}}`,
},
},
Spec: configv1alpha2.MeshRootCertificateSpec{},
Status: configv1alpha2.MeshRootCertificateStatus{
State: "complete",
RotationStage: "issuing",
State: constants.MRCStateComplete,
RotationStage: constants.MRCStageIssuing,
},
}

Expand Down Expand Up @@ -166,7 +152,6 @@ func TestBuildMeshRootCertificate(t *testing.T) {

meshRootCertificate, err := buildMeshRootCertificate(testPresetMeshRootCertificate)
assert.NoError(err)
assert.Contains(meshRootCertificate.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
assert.Equal(meshRootCertificate.Name, meshRootCertificateName)
assert.Equal(meshRootCertificate.Spec.Provider.Tresor.CA.SecretRef.Name, "osm-ca-bundle")
assert.Equal(meshRootCertificate.Spec.Provider.Tresor.CA.SecretRef.Namespace, testNamespace)
Expand Down Expand Up @@ -261,22 +246,10 @@ func TestCreateDefaultMeshConfig(t *testing.T) {
}

err := b.createDefaultMeshConfig()
if tc.expectErr {
assert.NotNil(err)
} else {
assert.Nil(err)
}
assert.Equal(tc.expectErr, err != nil)

_, err = b.configClient.ConfigV1alpha2().MeshConfigs(b.namespace).Get(context.TODO(), meshConfigName, metav1.GetOptions{})
if tc.expectDefaultMeshConfig {
if err == nil {
assert.Nil(err)
}
} else {
if err == nil {
assert.NotNil(err)
}
}
assert.Equal(tc.expectDefaultMeshConfig, err == nil)
})
}
}
Expand Down Expand Up @@ -329,10 +302,8 @@ func TestEnsureMeshConfig(t *testing.T) {
}

err := b.ensureMeshConfig()
if tc.expectErr {
assert.NotNil(err)
} else {
assert.Nil(err)
assert.Equal(tc.expectErr, err != nil)
if !tc.expectErr {
config, err := b.configClient.ConfigV1alpha2().MeshConfigs(b.namespace).Get(context.TODO(), meshConfigName, metav1.GetOptions{})
assert.Nil(err)
assert.Contains(config.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
Expand Down Expand Up @@ -386,22 +357,10 @@ func TestCreateMeshRootCertificateConfig(t *testing.T) {
}

err := b.createMeshRootCertificate()
if tc.expectErr {
assert.NotNil(err)
} else {
assert.Nil(err)
}
assert.Equal(tc.expectErr, err != nil)

_, err = b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).Get(context.TODO(), meshRootCertificateName, metav1.GetOptions{})
if tc.expectDefaultMeshRootCertificate {
if err == nil {
assert.Nil(err)
}
} else {
if err == nil {
assert.NotNil(err)
}
}
assert.Equal(tc.expectDefaultMeshRootCertificate, err == nil)
})
}
}
Expand All @@ -415,19 +374,12 @@ func TestEnsureMeshRootCertificate(t *testing.T) {
expectErr bool
}{
{
name: "MeshRootCertificate found with no last-applied annotation",
name: "MeshRootCertificate found",
namespace: testNamespace,
kubeClient: fakeKube.NewSimpleClientset(),
configClient: fakeConfig.NewSimpleClientset([]runtime.Object{testMeshRootCertificate}...),
expectErr: false,
},
{
name: "MeshRootCertificate found with last-applied annotation",
namespace: testNamespace,
kubeClient: fakeKube.NewSimpleClientset(),
configClient: fakeConfig.NewSimpleClientset([]runtime.Object{testMeshRootCertificateWithLastAppliedAnnotation}...),
expectErr: false,
},
{
name: "MeshRootCertificate not found but successfully created",
namespace: testNamespace,
Expand All @@ -454,14 +406,10 @@ func TestEnsureMeshRootCertificate(t *testing.T) {
}

err := b.ensureMeshRootCertificate()
if tc.expectErr {
assert.NotNil(err)
} else {
assert.Nil(err)
config, err := b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).Get(context.TODO(), meshRootCertificateName, metav1.GetOptions{})
assert.Nil(err)
assert.Contains(config.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
}
assert.Equal(tc.expectErr, err != nil)

_, err = b.configClient.ConfigV1alpha2().MeshRootCertificates(b.namespace).Get(context.TODO(), meshRootCertificateName, metav1.GetOptions{})
assert.Equal(tc.expectErr, err != nil)
})
}
}
Expand Down Expand Up @@ -522,11 +470,7 @@ func TestGetBootstrapPod(t *testing.T) {
assert.Nil(err)

_, err = b.getBootstrapPod()
if tc.expectErr {
assert.NotNil(err)
} else {
assert.Nil(err)
}
assert.Equal(tc.expectErr, err != nil)
})
}
}
Expand Down

0 comments on commit 95cc59a

Please sign in to comment.