Skip to content

Commit

Permalink
Add additional generators for kubeadmcontrolplane reconciliation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
detiber committed Dec 19, 2019
1 parent 2927e9f commit c50651e
Show file tree
Hide file tree
Showing 6 changed files with 589 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generator

import (
"context"
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
)

type KubeadmConfigGenerator struct{}

func (kcg *KubeadmConfigGenerator) GenerateKubeadmConfig(ctx context.Context, c client.Client, namespace, namePrefix, clusterName string, spec *bootstrapv1.KubeadmConfigSpec, owner *metav1.OwnerReference) (*corev1.ObjectReference, error) {
bootstrapConfig := &bootstrapv1.KubeadmConfig{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", namePrefix),
Namespace: namespace,
Labels: map[string]string{clusterv1.ClusterLabelName: clusterName},
},
Spec: *spec,
}
if owner != nil {
bootstrapConfig.SetOwnerReferences([]metav1.OwnerReference{*owner})
}
if err := c.Create(ctx, bootstrapConfig); err != nil {
return nil, errors.Wrap(err, "Failed to create bootstrap configuration")
}

bootstrapRef := &corev1.ObjectReference{
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "KubeadmConfig",
Name: bootstrapConfig.GetName(),
Namespace: bootstrapConfig.GetNamespace(),
UID: bootstrapConfig.GetUID(),
}

return bootstrapRef, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generator

import (
"context"
"reflect"
"testing"

"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"

"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func setupBootstrapScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
panic(err)
}
if err := bootstrapv1.AddToScheme(scheme); err != nil {
panic(err)
}
return scheme
}

func TestKubeadmConfigGenerator_GenerateKubeadmConfig(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
namespace string
namePrefix string
clusterName string
spec *bootstrapv1.KubeadmConfigSpec
owner *metav1.OwnerReference
want *corev1.ObjectReference
wantConfig *bootstrapv1.KubeadmConfig
wantErr bool
}{
{
name: "Generate successful",
namespace: "test",
namePrefix: "generate",
clusterName: "foo",
spec: &bootstrapv1.KubeadmConfigSpec{},
owner: &metav1.OwnerReference{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
Name: "testCluster",
},
want: &corev1.ObjectReference{
Name: "",
Namespace: "test",
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "KubeadmConfig",
},
wantConfig: &bootstrapv1.KubeadmConfig{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmConfig",
APIVersion: bootstrapv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "generate-",
Namespace: "test",
ResourceVersion: "1",
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
OwnerReferences: []metav1.OwnerReference{{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
Name: "testCluster",
}},
},
Spec: bootstrapv1.KubeadmConfigSpec{},
Status: bootstrapv1.KubeadmConfigStatus{},
},
wantErr: false,
},
{
name: "Generate successful, no owner",
namespace: "test",
namePrefix: "generate",
clusterName: "foo",
spec: &bootstrapv1.KubeadmConfigSpec{},
owner: nil,
want: &corev1.ObjectReference{
Name: "",
Namespace: "test",
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "KubeadmConfig",
},
wantConfig: &bootstrapv1.KubeadmConfig{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmConfig",
APIVersion: bootstrapv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "generate-",
Namespace: "test",
ResourceVersion: "1",
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
OwnerReferences: nil,
},
Spec: bootstrapv1.KubeadmConfigSpec{},
Status: bootstrapv1.KubeadmConfigStatus{},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient := fake.NewFakeClientWithScheme(setupBootstrapScheme())
kcg := &KubeadmConfigGenerator{}
got, err := kcg.GenerateKubeadmConfig(context.Background(), fakeClient, tt.namespace, tt.namePrefix, tt.clusterName, tt.spec, tt.owner)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateKubeadmConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenerateKubeadmConfig() got = %v, want %v", got, tt.want)
}
if !tt.wantErr {
bootstrapConfig := &bootstrapv1.KubeadmConfig{}
key := client.ObjectKey{Name: got.Name, Namespace: got.Namespace}
g.Expect(fakeClient.Get(context.Background(), key, bootstrapConfig)).To(gomega.Succeed())
g.Expect(bootstrapConfig).To(gomega.Equal(tt.wantConfig))
}
})
}
}
59 changes: 59 additions & 0 deletions controlplane/kubeadm/controllers/generator/machinegenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generator

import (
"context"
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

type MachineGenerator struct{}

func (mgg *MachineGenerator) GenerateMachine(ctx context.Context, c client.Client, namespace, namePrefix, clusterName, version string, infraRef, bootstrapRef *corev1.ObjectReference, labels map[string]string, owner *metav1.OwnerReference) error {
machine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", namePrefix),
Labels: labels,
Namespace: namespace,
},
Spec: clusterv1.MachineSpec{
ClusterName: clusterName,
Version: &version,
InfrastructureRef: *infraRef,
Bootstrap: clusterv1.Bootstrap{
ConfigRef: bootstrapRef,
},
},
}

if owner != nil {
machine.SetOwnerReferences([]metav1.OwnerReference{*owner})
}

if err := c.Create(ctx, machine); err != nil {
return errors.Wrap(err, "Failed to create machine")
}

return nil
}
Loading

0 comments on commit c50651e

Please sign in to comment.