Skip to content

Commit

Permalink
⚠️ Add support to auto-update external references
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Feb 14, 2020
1 parent ccd6f55 commit 22b8f14
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 99 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ help: ## Display this help

.PHONY: test
test: ## Run tests
## TODO(vincepri): Remove the fetch for external binaries once kubebuilder has a release.
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v ./...

.PHONY: test-integration
test-integration: ## Run integration tests
## TODO(vincepri): Remove the fetch for external binaries once kubebuilder has a release.
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v -tags=integration ./test/integration/...

.PHONY: test-capd-e2e-full
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/kubeadm/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
commonLabels:
cluster.x-k8s.io/v1alpha2: v1alpha2
cluster.x-k8s.io/v1alpha3: v1alpha3

# This kustomization.yaml is not intended to be run by itself,
# since it depends on service name and namespace that are out of this kustomize package.
# It should be run by config/
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/pkg/client/cluster/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"time"

"github.com/pkg/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/pkg/client/cluster/objectgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/pkg/internal/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package scheme

import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/pkg/internal/test/fake_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"

corev1 "k8s.io/api/core/v1"
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/pkg/internal/test/fake_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package test

import (
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
5 changes: 5 additions & 0 deletions controllers/cluster_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
Expand Down Expand Up @@ -62,6 +63,10 @@ func (r *ClusterReconciler) reconcilePhase(_ context.Context, cluster *clusterv1
func (r *ClusterReconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.Cluster, ref *corev1.ObjectReference) (external.ReconcileOutput, error) {
logger := r.Log.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)

if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
return external.ReconcileOutput{}, err
}

obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
Expand Down
15 changes: 9 additions & 6 deletions controllers/cluster_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "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"
"sigs.k8s.io/cluster-api/controllers/external"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand All @@ -50,7 +52,7 @@ func TestClusterReconcilePhases(t *testing.T) {
},
InfrastructureRef: &corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha3",
Kind: "InfrastructureConfig",
Kind: "InfrastructureMachine",
Name: "test",
},
},
Expand All @@ -76,7 +78,7 @@ func TestClusterReconcilePhases(t *testing.T) {
name: "returns no error if infra config is marked for deletion",
cluster: cluster,
infraRef: map[string]interface{}{
"kind": "InfrastructureConfig",
"kind": "InfrastructureMachine",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha3",
"metadata": map[string]interface{}{
"name": "test",
Expand All @@ -90,7 +92,7 @@ func TestClusterReconcilePhases(t *testing.T) {
name: "returns no error if infrastructure is marked ready on cluster",
cluster: cluster,
infraRef: map[string]interface{}{
"kind": "InfrastructureConfig",
"kind": "InfrastructureMachine",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha3",
"metadata": map[string]interface{}{
"name": "test",
Expand All @@ -104,7 +106,7 @@ func TestClusterReconcilePhases(t *testing.T) {
name: "returns error if infrastructure has the paused annotation",
cluster: cluster,
infraRef: map[string]interface{}{
"kind": "InfrastructureConfig",
"kind": "InfrastructureMachine",
"apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha3",
"metadata": map[string]interface{}{
"name": "test",
Expand All @@ -122,13 +124,14 @@ func TestClusterReconcilePhases(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed())
g.Expect(apiextensionsv1.AddToScheme(scheme.Scheme)).To(Succeed())

var c client.Client
if tt.infraRef != nil {
infraConfig := &unstructured.Unstructured{Object: tt.infraRef}
c = fake.NewFakeClientWithScheme(scheme.Scheme, tt.cluster, infraConfig)
c = fake.NewFakeClientWithScheme(scheme.Scheme, external.TestGenericInfrastructureCRD, tt.cluster, infraConfig)
} else {
c = fake.NewFakeClientWithScheme(scheme.Scheme, tt.cluster)
c = fake.NewFakeClientWithScheme(scheme.Scheme, external.TestGenericInfrastructureCRD, tt.cluster)
}
r := &ClusterReconciler{
Client: c,
Expand Down
13 changes: 13 additions & 0 deletions controllers/external/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

var (
Expand All @@ -30,6 +31,9 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Name: "genericmachines.bootstrap.cluster.x-k8s.io",
Labels: map[string]string{
clusterv1.GroupVersion.String(): "v1alpha3",
},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "bootstrap.cluster.x-k8s.io",
Expand Down Expand Up @@ -69,6 +73,9 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Name: "genericmachinetemplates.bootstrap.cluster.x-k8s.io",
Labels: map[string]string{
clusterv1.GroupVersion.String(): "v1alpha3",
},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "bootstrap.cluster.x-k8s.io",
Expand Down Expand Up @@ -108,6 +115,9 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Name: "genericmachines.infrastructure.cluster.x-k8s.io",
Labels: map[string]string{
clusterv1.GroupVersion.String(): "v1alpha3",
},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "infrastructure.cluster.x-k8s.io",
Expand Down Expand Up @@ -147,6 +157,9 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Name: "genericmachinetemplates.infrastructure.cluster.x-k8s.io",
Labels: map[string]string{
clusterv1.GroupVersion.String(): "v1alpha3",
},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "infrastructure.cluster.x-k8s.io",
Expand Down
5 changes: 5 additions & 0 deletions controllers/machine_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/pointer"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

Expand Down Expand Up @@ -77,6 +78,10 @@ func (r *MachineReconciler) reconcilePhase(_ context.Context, m *clusterv1.Machi
func (r *MachineReconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine, ref *corev1.ObjectReference) (external.ReconcileOutput, error) {
logger := r.Log.WithValues("machine", m.Name, "namespace", m.Namespace)

if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
return external.ReconcileOutput{}, err
}

obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
Expand Down
Loading

0 comments on commit 22b8f14

Please sign in to comment.