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 23, 2019
1 parent 8307778 commit 7180218
Show file tree
Hide file tree
Showing 368 changed files with 592 additions and 21,010 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
1 change: 1 addition & 0 deletions cmd/clusterctl/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ go_test(
"//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
12 changes: 10 additions & 2 deletions cmd/clusterctl/validation/validate_cluster_api_objects.go
Original file line number Diff line number Diff line change
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 common.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 Down
32 changes: 18 additions & 14 deletions cmd/clusterctl/validation/validate_cluster_api_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2/testutil"
Expand All @@ -36,7 +37,7 @@ import (

var c client.Client

func newClusterStatus(errorReason common.ClusterStatusError, errorMessage string) v1alpha2.ClusterStatus {
func newClusterStatus(errorReason *common.ClusterStatusError, errorMessage *string) v1alpha2.ClusterStatus {
return v1alpha2.ClusterStatus{
ErrorReason: errorReason,
ErrorMessage: errorMessage,
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 *common.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: common.ClusterStatusErrorPtr(common.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: common.ClusterStatusErrorPtr(common.CreateClusterError),
errorMessage: pointer.StringPtr("Failed to create cluster"),
expectErr: true,
},
}
Expand Down Expand Up @@ -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(
common.ClusterStatusErrorPtr(common.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
5 changes: 4 additions & 1 deletion pkg/apis/cluster/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["consts.go"],
srcs = [
"consts.go",
"pointers.go",
],
importpath = "sigs.k8s.io/cluster-api/pkg/apis/cluster/common",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright The Kubernetes Authors.
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.
Expand All @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Code generated by lister-gen. DO NOT EDIT.
package common

package v1beta1
// MachineStatusErrorPtr converts a MachineStatusError to a pointer.
func MachineStatusErrorPtr(v MachineStatusError) *MachineStatusError {
return &v
}

// LeaseListerExpansion allows custom methods to be added to
// LeaseLister.
type LeaseListerExpansion interface{}

// LeaseNamespaceListerExpansion allows custom methods to be added to
// LeaseNamespaceLister.
type LeaseNamespaceListerExpansion interface{}
// ClusterStatusErrorPtr converts a MachineStatusError to a pointer.
func ClusterStatusErrorPtr(v ClusterStatusError) *ClusterStatusError {
return &v
}
1 change: 1 addition & 0 deletions 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 Down
66 changes: 66 additions & 0 deletions pkg/apis/cluster/v1alpha2/cluster_phase_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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 v1alpha2

// ClusterPhase is a string representation of a Cluster Phase.
//
// This type is a high-level indicator of the status of the Cluster as it is provisioned,
// from the API user’s perspective.
//
// The value should not be interpreted by any software components as a reliable indication
// of the actual state of the Cluster, and controllers should not use the Cluster Phase field
// value when making decisions about what action to take.
//
// Controllers should always look at the actual state of the Cluster’s fields to make those decisions.
type ClusterPhase string

var (
// ClusterPhasePending is the first state a Cluster is assigned by
// Cluster API Cluster controller after being created.
ClusterPhasePending = ClusterPhase("pending")

// ClusterPhaseProvisioning is the state when the Cluster has a provider infrastructure
// object associated and can start provisioning.
ClusterPhaseProvisioning = ClusterPhase("provisioning")

// ClusterPhaseProvisioned is the state when its
// infrastructure has been created and configured.
ClusterPhaseProvisioned = ClusterPhase("provisioned")

// ClusterPhaseDeleting is the Cluster state when a delete
// request has been sent to the API Server,
// but its infrastructure has not yet been fully deleted.
ClusterPhaseDeleting = ClusterPhase("deleting")

// ClusterPhaseDeleted is the Cluster state when the object
// and the related infrastructure is deleted and
// ready to be garbage collected by the API Server.
ClusterPhaseDeleted = ClusterPhase("deleted")

// ClusterPhaseFailed is the Cluster state when the system
// might require user intervention.
ClusterPhaseFailed = ClusterPhase("failed")

// ClusterPhaseUnknown is returned if the Cluster state cannot be determined.
ClusterPhaseUnknown = ClusterPhase("")
)

// ClusterPhaseStringPtr is a helper method to convert ClusterPhase to a string pointer.
func ClusterPhaseStringPtr(p ClusterPhase) *string {
s := string(p)
return &s
}
Loading

0 comments on commit 7180218

Please sign in to comment.