Skip to content

Commit

Permalink
Add initial tests for kubeadmcontrolplane controller reconcile method
Browse files Browse the repository at this point in the history
- Add additional generators for kubeadmcontrolplane reconciliation testing
  • Loading branch information
detiber committed Dec 30, 2019
1 parent 83c7edf commit 190d38b
Show file tree
Hide file tree
Showing 7 changed files with 1,149 additions and 41 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,136 @@
/*
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"
"testing"

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

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

func TestGenerateKubeadmConfigWithOwner(t *testing.T) {
g := gomega.NewWithT(t)
g.Expect(bootstrapv1.AddToScheme(scheme.Scheme)).To(gomega.Succeed())
fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme)

namespace := "test"
namePrefix := "generate"
clusterName := "foo"
spec := &bootstrapv1.KubeadmConfigSpec{}
owner := &metav1.OwnerReference{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
Name: clusterName,
}
expectedReference := &corev1.ObjectReference{
Name: "",
Namespace: namespace,
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "KubeadmConfig",
}
expectedConfig := &bootstrapv1.KubeadmConfig{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmConfig",
APIVersion: bootstrapv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: namePrefix + "-",
Namespace: namespace,
ResourceVersion: "1",
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
OwnerReferences: []metav1.OwnerReference{*owner},
},
Spec: *spec,
Status: bootstrapv1.KubeadmConfigStatus{},
}

kcg := &KubeadmConfigGenerator{}
got, err := kcg.GenerateKubeadmConfig(
context.Background(),
fakeClient,
namespace,
namePrefix,
clusterName,
spec,
owner,
)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(got).To(gomega.Equal(expectedReference))

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

func TestGenerateKubeadmConfigWithoutOwner(t *testing.T) {
g := gomega.NewWithT(t)
g.Expect(bootstrapv1.AddToScheme(scheme.Scheme)).To(gomega.Succeed())
fakeClient := fake.NewFakeClientWithScheme(scheme.Scheme)

namespace := "test"
namePrefix := "generated"
clusterName := "foo"
spec := &bootstrapv1.KubeadmConfigSpec{}
expectedReference := &corev1.ObjectReference{
Name: "",
Namespace: namespace,
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "KubeadmConfig",
}
expectedConfig := &bootstrapv1.KubeadmConfig{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmConfig",
APIVersion: bootstrapv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: namePrefix + "-",
Namespace: namespace,
ResourceVersion: "1",
Labels: map[string]string{clusterv1.ClusterLabelName: "foo"},
},
Spec: *spec,
Status: bootstrapv1.KubeadmConfigStatus{},
}

kcg := &KubeadmConfigGenerator{}
got, err := kcg.GenerateKubeadmConfig(
context.Background(),
fakeClient,
namespace,
namePrefix,
clusterName,
spec,
nil,
)
g.Expect(err).NotTo(gomega.HaveOccurred())
g.Expect(got).To(gomega.Equal(expectedReference))

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(expectedConfig))
}
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 190d38b

Please sign in to comment.