Skip to content

Commit

Permalink
Integration test with example provider (kubernetes-sigs#755)
Browse files Browse the repository at this point in the history
This is the skeleton for the integration test
with example provider
  • Loading branch information
figo authored and k8s-ci-robot committed Feb 22, 2019
1 parent 6ea4182 commit d4d6eaf
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ docs/book/node_modules/

# Common editor / temporary files
*~
*.tmp

# rbac and manager config for example provider
config/ci/rbac/rbac_role_binding.yaml
config/ci/rbac/rbac_role.yaml
config/ci/manager/manager.yaml

1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT ?=60s
export KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT ?=60s

# Image URL to use all building/pushing image targets
IMG ?= gcr.io/k8s-cluster-api/cluster-api-controller:latest
export CONTROLLER_IMG ?= gcr.io/k8s-cluster-api/cluster-api-controller:latest
export EXAMPLE_PROVIDER_IMG ?= gcr.io/k8s-cluster-api/example-provider-controller:latest

all: test manager clusterctl

Expand Down Expand Up @@ -59,10 +60,11 @@ run: generate fmt vet ## Run against the configured Kubernetes cluster in ~/.kub
deploy: manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
kustomize build config/default | kubectl apply -f -


.PHONY: manifests
manifests: ## Generate manifests e.g. CRD, RBAC etc.
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go all
cp -f ./config/rbac/rbac*.yaml ./config/ci/rbac/
cp -f ./config/manager/manager*.yaml ./config/ci/manager/

.PHONY: fmt
fmt: ## Run go fmt against code
Expand Down Expand Up @@ -104,14 +106,24 @@ clean: ## Remove all generated files
rm -f bazel-*

.PHONY: docker-build
docker-build: generate fmt vet manifests ## Build the docker image
docker build . -t ${IMG}
docker-build: generate fmt vet manifests ## Build the docker image for controller-manager
docker build . -t ${CONTROLLER_IMG}
@echo "updating kustomize image patch file for manager resource"
sed -i.tmp -e 's@image: .*@image: '"${IMG}"'@' ./config/default/manager_image_patch.yaml
sed -i.tmp -e 's@image: .*@image: '"${CONTROLLER_IMG}"'@' ./config/default/manager_image_patch.yaml

.PHONY: docker-push
docker-push: ## Push the docker image
docker push ${IMG}
docker-push: docker-build ## Push the docker image
docker push "$(CONTROLLER_IMG)"

.PHONY: docker-build-ci
docker-build-ci: generate fmt vet manifests ## Build the docker image for example provider
docker build . -f ./pkg/provider/example/container/Dockerfile -t ${EXAMPLE_PROVIDER_IMG}
@echo "updating kustomize image patch file for ci"
sed -i.tmp -e 's@image: .*@image: '"${EXAMPLE_PROVIDER_IMG}"'@' ./config/ci/manager_image_patch.yaml

.PHONY: docker-push-ci
docker-push-ci: docker-build-ci ## Build the docker image for ci
docker push "$(EXAMPLE_PROVIDER_IMG)"

.PHONY: verify
verify:
Expand Down
27 changes: 27 additions & 0 deletions cmd/example-provider/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "sigs.k8s.io/cluster-api/cmd/example-provider",
visibility = ["//visibility:private"],
deps = [
"//pkg/apis:go_default_library",
"//pkg/apis/cluster/common:go_default_library",
"//pkg/client/clientset_generated/clientset:go_default_library",
"//pkg/controller/cluster:go_default_library",
"//pkg/controller/machine:go_default_library",
"//pkg/provider/example/actuators/cluster:go_default_library",
"//pkg/provider/example/actuators/machine:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/config:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/manager:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/runtime/signals:go_default_library",
],
)

go_binary(
name = "example-provider",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
74 changes: 74 additions & 0 deletions cmd/example-provider/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 main

import (
"flag"

"k8s.io/klog"
clusterapis "sigs.k8s.io/cluster-api/pkg/apis"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
capicluster "sigs.k8s.io/cluster-api/pkg/controller/cluster"
capimachine "sigs.k8s.io/cluster-api/pkg/controller/machine"
"sigs.k8s.io/cluster-api/pkg/provider/example/actuators/cluster"
"sigs.k8s.io/cluster-api/pkg/provider/example/actuators/machine"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
)

func main() {
klog.InitFlags(nil)
flag.Set("logtostderr", "true")
flag.Parse()

cfg := config.GetConfigOrDie()

// Setup a Manager
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
klog.Fatalf("Failed to set up controller manager: %v", err)
}

cs, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Failed to create client from configuration: %v", err)
}

recorder := mgr.GetRecorder("clusterapi-controller")

// Initialize cluster actuator.
clusterActuator, _ := cluster.NewClusterActuator(cs.ClusterV1alpha1(), recorder)

// Initialize machine actuator.
machineActuator, _ := machine.NewMachineActuator(cs.ClusterV1alpha1(), recorder)

// Register cluster deployer
common.RegisterClusterProvisioner("example", clusterActuator)

if err := clusterapis.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}

capimachine.AddWithActuator(mgr, machineActuator)
capicluster.AddWithActuator(mgr, clusterActuator)

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
klog.Fatalf("Failed to run manager: %v", err)
}
}
16 changes: 16 additions & 0 deletions config/ci/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Adds namespace to all resources.
namespace: provider-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: provider-

bases:
- ./rbac/
- ./manager/

patchesStrategicMerge:
- manager_image_patch.yaml
8 changes: 8 additions & 0 deletions config/ci/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- manager.yaml

11 changes: 11 additions & 0 deletions config/ci/manager_image_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- image: gcr.io/k8s-cluster-api/example-provider-controller:latest
name: manager
8 changes: 8 additions & 0 deletions config/ci/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- rbac_role_binding.yaml
- rbac_role.yaml
12 changes: 12 additions & 0 deletions config/crds/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- cluster_v1alpha1_cluster.yaml
- cluster_v1alpha1_machine.yaml
- cluster_v1alpha1_machineclass.yaml
- cluster_v1alpha1_machinedeployment.yaml
- cluster_v1alpha1_machineset.yaml

15 changes: 15 additions & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Adds namespace to all resources.
namespace: cluster-api-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: cluster-api-

bases:
- ../crds/
- ../rbac/
- ../manager/

31 changes: 0 additions & 31 deletions config/kustomization.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- manager.yaml

9 changes: 9 additions & 0 deletions config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- rbac_role_binding.yaml
- rbac_role.yaml

9 changes: 9 additions & 0 deletions config/rbac/rbac_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ rules:
- update
- patch
- delete
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
- create
- apiGroups:
- cluster.k8s.io
resources:
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
var AddToManagerFuncs []func(manager.Manager) error

// AddToManager adds all Controllers to the Manager
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create
func AddToManager(m manager.Manager) error {
for _, f := range AddToManagerFuncs {
if err := f(m); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/machine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//pkg/util:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
Expand Down
14 changes: 14 additions & 0 deletions pkg/provider/example/actuators/cluster/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["clusteractuator.go"],
importpath = "sigs.k8s.io/cluster-api/pkg/provider/example/actuators/cluster",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/cluster/v1alpha1:go_default_library",
"//pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/client-go/tools/record:go_default_library",
],
)
50 changes: 50 additions & 0 deletions pkg/provider/example/actuators/cluster/clusteractuator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 cluster

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
)

// Actuator is responsible for performing cluster reconciliation
type Actuator struct {
clusterV1alpha1 clusterv1alpha1.ClusterV1alpha1Interface
recorder record.EventRecorder
}

// NewClusterActuator creates a new cluster actuator
func NewClusterActuator(clusterV1alpha1 clusterv1alpha1.ClusterV1alpha1Interface, recorder record.EventRecorder) (*Actuator, error) {
return &Actuator{
clusterV1alpha1: clusterV1alpha1,
recorder: recorder,
}, nil
}

// Reconcile will create or update the cluster
func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
a.recorder.Event(cluster, corev1.EventTypeWarning, "cluster-api", "clusteractuator Reconcile invoked")
return nil
}

// Delete deletes a cluster and is invoked by the Cluster Controller
func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {
a.recorder.Event(cluster, corev1.EventTypeWarning, "cluster-api", "clusteractuator Delete invoked")
return nil
}
Loading

0 comments on commit d4d6eaf

Please sign in to comment.