Skip to content

Commit

Permalink
Cluster controller and types for v1alpha2
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jul 24, 2019
1 parent 6afa7ae commit 894a68c
Show file tree
Hide file tree
Showing 390 changed files with 651 additions and 21,110 deletions.
4 changes: 2 additions & 2 deletions cmd/clusterctl/clusterdeployer/clusterclient/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,8 @@ func (c *client) WaitForResourceStatuses() error {
klog.V(10).Info("retrying: cluster status is empty")
return false, nil
}
if cluster.Status.ProviderStatus == nil {
klog.V(10).Info("retrying: cluster.Status.ProviderStatus is not set")
if !cluster.Status.InfrastructureReady {
klog.V(10).Info("retrying: cluster.Status.InfrastructureReady is false")
return false, nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/clusterctl/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ go_library(
importpath = "sigs.k8s.io/cluster-api/cmd/clusterctl/validation",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/cluster/common:go_default_library",
"//pkg/apis/cluster/v1alpha2:go_default_library",
"//pkg/controller/noderefutil:go_default_library",
"//pkg/errors:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
Expand All @@ -30,14 +30,15 @@ go_test(
embed = [":go_default_library"],
deps = [
"//pkg/apis:go_default_library",
"//pkg/apis/cluster/common:go_default_library",
"//pkg/apis/cluster/v1alpha2:go_default_library",
"//pkg/apis/cluster/v1alpha2/testutil:go_default_library",
"//pkg/errors:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/envtest:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/manager:go_default_library",
Expand Down
16 changes: 12 additions & 4 deletions cmd/clusterctl/validation/validate_cluster_api_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
clusterv1alpha2 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
"sigs.k8s.io/cluster-api/pkg/controller/noderefutil"
capierrors "sigs.k8s.io/cluster-api/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -73,9 +73,17 @@ func getClusterObject(ctx context.Context, c client.Reader, clusterName string,

func validateClusterObject(w io.Writer, cluster *v1alpha2.Cluster) error {
fmt.Fprintf(w, "Checking cluster object %q... ", cluster.Name)
if cluster.Status.ErrorReason != "" || cluster.Status.ErrorMessage != "" {
if cluster.Status.ErrorReason != nil || cluster.Status.ErrorMessage != nil {
var reason capierrors.ClusterStatusError
if cluster.Status.ErrorReason != nil {
reason = *cluster.Status.ErrorReason
}
var message string
if cluster.Status.ErrorMessage != nil {
message = *cluster.Status.ErrorMessage
}
fmt.Fprintf(w, "FAIL\n")
fmt.Fprintf(w, "\t[%v]: %s\n", cluster.Status.ErrorReason, cluster.Status.ErrorMessage)
fmt.Fprintf(w, "\t[%v]: %s\n", reason, message)
return errors.Errorf("cluster %q failed the validation", cluster.Name)
}
fmt.Fprintf(w, "PASS\n")
Expand All @@ -98,7 +106,7 @@ func validateMachineObjects(ctx context.Context, w io.Writer, machines *v1alpha2
func validateMachineObject(ctx context.Context, w io.Writer, machine v1alpha2.Machine, client client.Client) bool {
fmt.Fprintf(w, "Checking machine object %q... ", machine.Name)
if machine.Status.ErrorReason != nil || machine.Status.ErrorMessage != nil {
var reason common.MachineStatusError
var reason capierrors.MachineStatusError
if machine.Status.ErrorReason != nil {
reason = *machine.Status.ErrorReason
}
Expand Down
44 changes: 24 additions & 20 deletions cmd/clusterctl/validation/validate_cluster_api_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,32 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"k8s.io/utils/pointer"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2/testutil"
capierrors "sigs.k8s.io/cluster-api/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

var c client.Client

func newClusterStatus(errorReason common.ClusterStatusError, errorMessage string) v1alpha2.ClusterStatus {
func newClusterStatus(errorReason *capierrors.ClusterStatusError, errorMessage *string) v1alpha2.ClusterStatus {
return v1alpha2.ClusterStatus{
ErrorReason: errorReason,
ErrorMessage: errorMessage,
}
}

func newMachineStatus(nodeRef *v1.ObjectReference, errorReason *common.MachineStatusError, errorMessage *string) v1alpha2.MachineStatus {
func newMachineStatus(nodeRef *v1.ObjectReference, errorReason *capierrors.MachineStatusError, errorMessage *string) v1alpha2.MachineStatus {
return v1alpha2.MachineStatus{
NodeRef: nodeRef,
ErrorReason: errorReason,
ErrorMessage: errorMessage,
}
}

func getMachineWithError(machineName, namespace string, nodeRef *v1.ObjectReference, errorReason *common.MachineStatusError, errorMessage *string) v1alpha2.Machine {
func getMachineWithError(machineName, namespace string, nodeRef *v1.ObjectReference, errorReason *capierrors.MachineStatusError, errorMessage *string) v1alpha2.Machine {
return v1alpha2.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: machineName,
Expand Down Expand Up @@ -225,32 +226,32 @@ func TestGetClusterObjectWithMoreThanOneCluster(t *testing.T) {
func TestValidateClusterObject(t *testing.T) {
var testcases = []struct {
name string
errorReason common.ClusterStatusError
errorMessage string
errorReason *capierrors.ClusterStatusError
errorMessage *string
expectErr bool
}{
{
name: "Cluster has no error",
errorReason: "",
errorMessage: "",
errorReason: nil,
errorMessage: nil,
expectErr: false,
},
{
name: "Cluster has error reason",
errorReason: common.CreateClusterError,
errorMessage: "",
errorReason: capierrors.ClusterStatusErrorPtr(capierrors.CreateClusterError),
errorMessage: nil,
expectErr: true,
},
{
name: "Cluster has error message",
errorReason: "",
errorMessage: "Failed to create cluster",
errorReason: nil,
errorMessage: pointer.StringPtr("Failed to create cluster"),
expectErr: true,
},
{
name: "Cluster has error reason and message",
errorReason: common.CreateClusterError,
errorMessage: "Failed to create cluster",
errorReason: capierrors.ClusterStatusErrorPtr(capierrors.CreateClusterError),
errorMessage: pointer.StringPtr("Failed to create cluster"),
expectErr: true,
},
}
Expand Down Expand Up @@ -289,12 +290,12 @@ func TestValidateMachineObjects(t *testing.T) {
defer c.Delete(context.TODO(), &testNode)

testNodeRef := v1.ObjectReference{Kind: "Node", Name: testNodeName}
machineErrorReason := common.CreateMachineError
machineErrorReason := capierrors.CreateMachineError
machineErrorMessage := "Failed to create machine"
var testcases = []struct {
name string
nodeRef *v1.ObjectReference
errorReason *common.MachineStatusError
errorReason *capierrors.MachineStatusError
errorMessage *string
expectErr bool
}{
Expand Down Expand Up @@ -443,7 +444,7 @@ func TestValidateClusterAPIObjectsOutput(t *testing.T) {
testNodeRef2 := v1.ObjectReference{Kind: "Node", Name: testNode2Name}
testNodeRefNotReady := v1.ObjectReference{Kind: "Node", Name: testNodeNotReadyName}
testNodeRefNotExist := v1.ObjectReference{Kind: "Node", Name: "test-node-not-exist"}
machineErrorReason := common.CreateMachineError
machineErrorReason := capierrors.CreateMachineError
machineErrorMessage := "Failed to create machine"

var testcases = []struct {
Expand All @@ -465,9 +466,12 @@ func TestValidateClusterAPIObjectsOutput(t *testing.T) {
outputFileName: "validate-cluster-api-object-output-pass.golden",
},
{
name: "Failed to validate cluster object",
namespace: "validate-cluster-objects-errors",
clusterStatus: newClusterStatus(common.CreateClusterError, "Failed to create cluster"),
name: "Failed to validate cluster object",
namespace: "validate-cluster-objects-errors",
clusterStatus: newClusterStatus(
capierrors.ClusterStatusErrorPtr(capierrors.CreateClusterError),
pointer.StringPtr("Failed to create cluster"),
),
machine1Status: newMachineStatus(&testNodeRef1, nil, nil),
machine2Status: newMachineStatus(&testNodeRef2, nil, nil),
expectErr: true,
Expand Down
2 changes: 0 additions & 2 deletions cmd/example-provider/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//pkg/apis: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",
"//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",
Expand Down
15 changes: 1 addition & 14 deletions cmd/example-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ import (

"k8s.io/klog"
clusterapis "sigs.k8s.io/cluster-api/pkg/apis"
"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/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()
Expand All @@ -43,22 +40,12 @@ func main() {
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.GetEventRecorderFor("clusterapi-controller")

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

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

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

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
klog.Fatalf("Failed to run manager: %v", err)
Expand Down
88 changes: 56 additions & 32 deletions config/crds/cluster.x-k8s.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,63 @@ spec:
- serviceDomain
- services
type: object
providerSpec:
description: Provider-specific serialized configuration to use during
cluster creation. It is recommended that providers maintain their
own versioned API types that should be serialized/deserialized from
this field.
infrastructureRef:
description: InfrastructureRef is a reference to a provider-specific
resource that holds the details for provisioning infrastructure for
a cluster in said provider.
properties:
value:
description: Value is an inlined, serialized representation of the
resource configuration. It is recommended that providers maintain
their own versioned API types that should be serialized/deserialized
from this field, akin to component config.
type: object
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of an
entire object, this string should contain a valid JSON/Go field
access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen only
to have some well-defined way of referencing a part of an object.
TODO: this design is not final and this field is subject to change
in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference is
made, if any. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
type: object
status:
description: / [ClusterStatus] ClusterStatus defines the observed state
of Cluster
properties:
apiEndpoints:
apiEndpoint:
description: APIEndpoint represents the endpoint to communicate with
the IP.
items:
description: / [APIEndpoint] APIEndpoint represents a reachable Kubernetes
API endpoint.
properties:
host:
description: The hostname on which the API server is serving.
type: string
port:
description: The port on which the API server is serving.
type: integer
required:
- host
- port
type: object
type: array
properties:
host:
description: The hostname on which the API server is serving.
type: string
port:
description: The port on which the API server is serving.
type: integer
required:
- host
- port
type: object
errorMessage:
description: If set, indicates that there is a problem reconciling the
state, and will be set to a descriptive error message.
Expand All @@ -110,11 +131,14 @@ spec:
state, and will be set to a token value suitable for programmatic
interpretation.
type: string
providerStatus:
description: Provider-specific status. It is recommended that providers
maintain their own versioned API types that should be serialized/deserialized
from this field.
type: object
infrastructureReady:
description: InfrastructureReady is the state of the infrastructure
provider.
type: boolean
phase:
description: Phase represents the current phase of cluster actuation.
E.g. Pending, Running, Terminating, Failed etc.
type: string
type: object
type: object
versions:
Expand Down
8 changes: 0 additions & 8 deletions pkg/apis/cluster/common/BUILD.bazel

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/apis/cluster/v1alpha2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"cluster_phase_types.go",
"cluster_types.go",
"common_types.go",
"defaults.go",
Expand All @@ -17,7 +18,7 @@ go_library(
importpath = "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/cluster/common:go_default_library",
"//pkg/errors:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/validation:go_default_library",
Expand Down
Loading

0 comments on commit 894a68c

Please sign in to comment.