Skip to content

Commit

Permalink
Instance spec as part of plan execution CRD (#705)
Browse files Browse the repository at this point in the history
* Instance spec as part of instance controller

* Formatting
  • Loading branch information
alenkacz authored and kudo-ci committed Aug 12, 2019
1 parent 9d5c0da commit 750c4c9
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 56 deletions.
1 change: 1 addition & 0 deletions config/crds/kudo_v1alpha1_instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
description: Operator specifies a reference to a specific Operator object.
type: object
parameters:
description: 'TODO: this is deprecated and should not be used'
type: object
type: object
status:
Expand Down
27 changes: 27 additions & 0 deletions config/crds/kudo_v1alpha1_planexecution.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,36 @@ spec:
description: This flag tells the controller to suspend subsequent executions,
it does not apply to already started executions. Defaults to false.
type: boolean
template:
properties:
dependencies:
items:
properties:
referenceName:
description: Name specifies the name of the dependency. Referenced
via defaults.config.
type: string
version:
description: 'Version captures the requirements for what versions
of the above object are allowed. Example: ^3.1.4'
type: string
required:
- referenceName
- version
type: object
type: array
operatorVersion:
description: Operator specifies a reference to a specific Operator
object.
type: object
parameters:
description: 'TODO: this is deprecated and should not be used'
type: object
type: object
required:
- planName
- instance
- template
type: object
status:
properties:
Expand Down
38 changes: 38 additions & 0 deletions config/crds/kudo_v1alpha1_teststep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ spec:
delete:
description: Objects to delete at the beginning of the test step.
items:
properties:
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: array
index:
Expand All @@ -32,6 +64,11 @@ spec:
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
type: string
kubectl:
description: Kubectl commands to run at the start of the test
items:
type: string
type: array
metadata:
description: Override the default metadata. Set labels or override the test
step name.
Expand All @@ -42,6 +79,7 @@ spec:
type: boolean
required:
- unitTest
- kubectl
version: v1alpha1
status:
acceptedNames:
Expand Down
51 changes: 48 additions & 3 deletions config/crds/kudo_v1alpha1_testsuite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ spec:
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
type: string
artifactsDir:
description: The directory to output artifacts to (current working directory
if not specified).
type: string
crdDir:
description: Path to CRDs to install before running tests.
type: string
Expand All @@ -27,16 +31,44 @@ spec:
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
type: string
manifestsDir:
description: Path to manifests to install before running tests.
kindConfig:
description: Path to the KIND configuration file to use.
type: string
kindContext:
description: KIND context to use.
type: string
kubectl:
description: Kubectl commands to run before running any tests.
items:
type: string
type: array
manifestDirs:
description: Paths to directories containing manifests to install before
running tests.
items:
type: string
type: array
metadata:
description: Set labels or the test suite name.
type: object
parallel:
description: 'The maximum number of tests to run at once (default: 8).'
format: int64
type: integer
skipClusterDelete:
description: If set, do not delete the mocked control plane or kind cluster.
type: boolean
skipDelete:
description: If set, do not delete the resources after running the tests
(implies SkipClusterDelete).
type: boolean
startControlPlane:
description: Whether or not to start a local etcd and kubernetes API server
for the tests.
type: boolean
startKIND:
description: Whether or not to start a local kind cluster for the tests.
type: boolean
startKUDO:
description: Whether or not to start the KUDO controller for the tests.
type: boolean
Expand All @@ -45,12 +77,25 @@ spec:
items:
type: string
type: array
timeout:
description: Override the default timeout of 30 seconds (in seconds).
format: int64
type: integer
required:
- crdDir
- manifestsDir
- manifestDirs
- testDirs
- startControlPlane
- startKIND
- kindConfig
- kindContext
- startKUDO
- skipDelete
- skipClusterDelete
- timeout
- parallel
- artifactsDir
- kubectl
version: v1alpha1
status:
acceptedNames:
Expand Down
2 changes: 1 addition & 1 deletion hack/update_manifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ else
fi

go run "$CONTROLLER_GEN_DIR"/cmd/controller-gen/main.go rbac
go run "$CONTROLLER_GEN_DIR"/cmd/controller-gen/main.go crd
go run "$CONTROLLER_GEN_DIR"/cmd/controller-gen/main.go crd --domain dev
1 change: 1 addition & 0 deletions pkg/apis/kudo/v1alpha1/planexecution_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type PlanExecutionSpec struct {

PlanName string `json:"planName"`
Instance corev1.ObjectReference `json:"instance"`
Template InstanceSpec `json:"template"`
}

// PlanExecutionStatus defines the observed state of PlanExecution
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/kudo/v1alpha1/zz_generated.deepcopy.go

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

55 changes: 29 additions & 26 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"k8s.io/client-go/tools/record"

kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/kudobuilder/kudo/pkg/controller/planexecution"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -309,9 +307,8 @@ func instanceEventFilter(mgr manager.Manager) predicate.Funcs {

func createPlan(r *ReconcileInstance, planName string, instance *kudov1alpha1.Instance) error {
ctx := context.TODO()
gvk, _ := apiutil.GVKForObject(instance, r.scheme)

planExecution := planexecution.New(gvk.Kind, instance, planName)
planExecution := newPlanExecution(instance, planName, r.scheme)
return createPlanExecution(ctx, instance, planExecution, r, planName)
}

Expand Down Expand Up @@ -339,32 +336,11 @@ func createPlanExecution(ctx context.Context, instance *kudov1alpha1.Instance, p
// most likely... any use of this function is wrong... all plans should be created in reconcile
func createPlanOld(mgr manager.Manager, planName string, instance *kudov1alpha1.Instance) error {
ctx := context.TODO()
gvk, _ := apiutil.GVKForObject(instance, mgr.GetScheme())
recorder := mgr.GetEventRecorderFor("instance-controller")
log.Printf("Creating PlanExecution of plan %s for instance %s", planName, instance.Name)
recorder.Event(instance, "Normal", "CreatePlanExecution", fmt.Sprintf("Creating \"%v\" plan execution", planName))

ref := corev1.ObjectReference{
Kind: gvk.Kind,
Name: instance.Name,
Namespace: instance.Namespace,
}

planExecution := kudov1alpha1.PlanExecution{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-%v-%v", instance.Name, planName, time.Now().Nanosecond()),
Namespace: instance.GetNamespace(),
// TODO: Should also add one for Operator in here as well.
Labels: map[string]string{
kudo.OperatorVersionAnnotation: instance.Spec.OperatorVersion.Name,
kudo.InstanceLabel: instance.Name,
},
},
Spec: kudov1alpha1.PlanExecutionSpec{
Instance: ref,
PlanName: planName,
},
}
planExecution := newPlanExecution(instance, planName, mgr.GetScheme())

// Make this instance the owner of the PlanExecution
if err := controllerutil.SetControllerReference(instance, &planExecution, mgr.GetScheme()); err != nil {
Expand Down Expand Up @@ -499,3 +475,30 @@ func parameterDifference(old, new map[string]string) map[string]string {

return diff
}

// newPlanExecution creates a PlanExecution based on the kind and instance for a given planName
func newPlanExecution(instance *kudov1alpha1.Instance, planName string, scheme *runtime.Scheme) kudov1alpha1.PlanExecution {
gvk, _ := apiutil.GVKForObject(instance, scheme)
ref := corev1.ObjectReference{
Kind: gvk.Kind,
Name: instance.Name,
Namespace: instance.Namespace,
}
planExecution := kudov1alpha1.PlanExecution{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-%v-%v", instance.Name, planName, time.Now().Nanosecond()),
Namespace: instance.GetNamespace(),
// TODO: Should also add one for Operator in here as well.
Labels: map[string]string{
kudo.OperatorVersionAnnotation: instance.Spec.OperatorVersion.Name,
kudo.InstanceLabel: instance.Name,
},
},
Spec: kudov1alpha1.PlanExecutionSpec{
Instance: ref,
PlanName: planName,
Template: instance.Spec,
},
}
return planExecution
}
26 changes: 0 additions & 26 deletions pkg/controller/planexecution/planexecution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"log"
"strconv"
"strings"
"time"

"github.com/kudobuilder/kudo/pkg/util/kudo"

Expand Down Expand Up @@ -626,28 +625,3 @@ func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", " ")
return string(s)
}

// New creates a PlanExecution based on the kind and instance for a given planName
func New(kind string, instance *kudov1alpha1.Instance, planName string) kudov1alpha1.PlanExecution {
ref := corev1.ObjectReference{
Kind: kind,
Name: instance.Name,
Namespace: instance.Namespace,
}
planExecution := kudov1alpha1.PlanExecution{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-%v-%v", instance.Name, planName, time.Now().Nanosecond()),
Namespace: instance.GetNamespace(),
// TODO: Should also add one for Operator in here as well.
Labels: map[string]string{
kudo.OperatorVersionAnnotation: instance.Spec.OperatorVersion.Name,
kudo.InstanceLabel: instance.Name,
},
},
Spec: kudov1alpha1.PlanExecutionSpec{
Instance: ref,
PlanName: planName,
},
}
return planExecution
}

0 comments on commit 750c4c9

Please sign in to comment.