Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ import (
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/controllers/metrics"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -111,6 +113,7 @@ func (r *ClusterReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, reterr e
defer func() {
// Always reconcile the Status.Phase field.
r.reconcilePhase(ctx, cluster)
r.reconcileMetrics(ctx, cluster)

// Always attempt to Patch the Cluster object and status after each reconciliation.
if err := patchHelper.Patch(ctx, cluster); err != nil {
Expand Down Expand Up @@ -164,6 +167,35 @@ func (r *ClusterReconciler) reconcile(ctx context.Context, cluster *clusterv1.Cl
return res, kerrors.NewAggregate(errs)
}

func (r *ClusterReconciler) reconcileMetrics(_ context.Context, cluster *clusterv1.Cluster) {

if cluster.Status.ControlPlaneInitialized {
metrics.ClusterControlPlaneReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(1)
} else {
metrics.ClusterControlPlaneReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(0)
}

if cluster.Status.InfrastructureReady {
metrics.ClusterInfrastructureReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(1)
} else {
metrics.ClusterInfrastructureReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(0)
}

// TODO: [wfernandes] pass context here
_, err := secret.Get(r.Client, cluster, secret.Kubeconfig)
if err != nil {
metrics.ClusterKubeconfigReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(0)
} else {
metrics.ClusterKubeconfigReady.WithLabelValues(cluster.Name, cluster.Namespace).Set(1)
}

if cluster.Status.ErrorReason != nil || cluster.Status.ErrorMessage != nil {
metrics.ClusterErrorSet.WithLabelValues(cluster.Name, cluster.Namespace).Set(1)
} else {
metrics.ClusterErrorSet.WithLabelValues(cluster.Name, cluster.Namespace).Set(0)
}
}

// reconcileDelete handles cluster deletion.
func (r *ClusterReconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster) (reconcile.Result, error) {
logger := r.Log.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)
Expand Down
252 changes: 179 additions & 73 deletions controllers/cluster_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,90 +20,196 @@ import (
"context"
"testing"

. "github.com/onsi/gomega"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func TestClusterReconciler_reconcileKubeconfig(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster",
},
Status: clusterv1.ClusterStatus{
APIEndpoints: []clusterv1.APIEndpoint{{
Host: "1.2.3.4",
Port: 0,
}},
},
}

tests := []struct {
name string
cluster *clusterv1.Cluster
secret *corev1.Secret
wantErr bool
wantRequeue bool
}{
{
name: "cluster not provisioned, apiEndpoint is not set",
cluster: &clusterv1.Cluster{},
wantErr: false,
},
{
name: "kubeconfig secret found",
cluster: cluster,
secret: &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster-kubeconfig",
func TestClusterReconcilePhases(t *testing.T) {
t.Run("reconcile infrastructure", func(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-namespace",
},
Status: clusterv1.ClusterStatus{
APIEndpoints: []clusterv1.APIEndpoint{{
Host: "1.2.3.4",
Port: 0,
}},
InfrastructureReady: true,
},
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha2",
Kind: "InfrastructureConfig",
Name: "test",
},
},
}

tests := []struct {
name string
cluster *clusterv1.Cluster
infraRef map[string]interface{}
expectErr bool
}{
{
name: "returns no error if infrastructure ref is nil",
cluster: &clusterv1.Cluster{ObjectMeta: v1.ObjectMeta{Name: "test-cluster", Namespace: "test-namespace"}},
expectErr: false,
},
{
name: "returns error if unable to reconcile infrastructure ref",
cluster: cluster,
expectErr: true,
},
{
name: "returns no error if infra config is marked for deletion",
cluster: cluster,
infraRef: map[string]interface{}{
"kind": "InfrastructureConfig",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha2",
"metadata": map[string]interface{}{
"name": "test",
"namespace": "test-namespace",
"deletionTimestamp": "sometime",
},
},
expectErr: false,
},
wantErr: false,
},
{
name: "kubeconfig secret not found, should return RequeueAfterError",
cluster: cluster,
wantErr: true,
wantRequeue: true,
},
{
name: "invalid ca secret, should return error",
cluster: cluster,
secret: &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster-ca",
{
name: "returns no error if infrastructure is marked ready on cluster",
cluster: cluster,
infraRef: map[string]interface{}{
"kind": "InfrastructureConfig",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha2",
"metadata": map[string]interface{}{
"name": "test",
"namespace": "test-namespace",
"deletionTimestamp": "sometime",
},
},
expectErr: false,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := clusterv1.AddToScheme(scheme.Scheme)
if err != nil {
t.Fatal(err)
}

c := fake.NewFakeClient(tt.cluster)
if tt.secret != nil {
c = fake.NewFakeClient(tt.cluster, tt.secret)
}
r := &ClusterReconciler{
Client: c,
}
err = r.reconcileKubeconfig(context.Background(), tt.cluster)
if (err != nil) != tt.wantErr {
t.Errorf("reconcileKubeconfig() error = %v, wantErr %v", err, tt.wantErr)
}

_, hasRequeErr := errors.Cause(err).(capierrors.HasRequeueAfterError)
if tt.wantRequeue != hasRequeErr {
t.Errorf("expected RequeAfterError = %v, got %v", tt.wantRequeue, hasRequeErr)
}
})
}
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterTestingT(t)
err := clusterv1.AddToScheme(scheme.Scheme)
if err != nil {
t.Fatal(err)
}

var c client.Client
if tt.infraRef != nil {
infraConfig := &unstructured.Unstructured{Object: tt.infraRef}
c = fake.NewFakeClient(tt.cluster, infraConfig)
} else {
c = fake.NewFakeClient(tt.cluster)
}
r := &ClusterReconciler{
Client: c,
Log: log.Log,
}

err = r.reconcileInfrastructure(context.Background(), tt.cluster)
if tt.expectErr {
Expect(err).To(HaveOccurred())
} else {
Expect(err).ToNot(HaveOccurred())
}
})
}

})

t.Run("reconcile kubeconfig", func(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster",
},
Status: clusterv1.ClusterStatus{
APIEndpoints: []clusterv1.APIEndpoint{{
Host: "1.2.3.4",
Port: 0,
}},
},
}

tests := []struct {
name string
cluster *clusterv1.Cluster
secret *corev1.Secret
wantErr bool
wantRequeue bool
}{
{
name: "cluster not provisioned, apiEndpoint is not set",
cluster: &clusterv1.Cluster{},
wantErr: false,
},
{
name: "kubeconfig secret found",
cluster: cluster,
secret: &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster-kubeconfig",
},
},
wantErr: false,
},
{
name: "kubeconfig secret not found, should return RequeueAfterError",
cluster: cluster,
wantErr: true,
wantRequeue: true,
},
{
name: "invalid ca secret, should return error",
cluster: cluster,
secret: &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: "test-cluster-ca",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterTestingT(t)
err := clusterv1.AddToScheme(scheme.Scheme)
if err != nil {
t.Fatal(err)
}

c := fake.NewFakeClient(tt.cluster)
if tt.secret != nil {
c = fake.NewFakeClient(tt.cluster, tt.secret)
}
r := &ClusterReconciler{
Client: c,
}
err = r.reconcileKubeconfig(context.Background(), tt.cluster)
if (err != nil) != tt.wantErr {
t.Errorf("reconcileKubeconfig() error = %v, wantErr %v", err, tt.wantErr)
}

_, hasRequeErr := errors.Cause(err).(capierrors.HasRequeueAfterError)
if tt.wantRequeue != hasRequeErr {
t.Errorf("expected RequeAfterError = %v, got %v", tt.wantRequeue, hasRequeErr)
}
})
}
})
}
Loading