diff --git a/api/v1alpha1/runner_types.go b/api/v1alpha1/runner_types.go index 234d9aaa..93cc30d0 100644 --- a/api/v1alpha1/runner_types.go +++ b/api/v1alpha1/runner_types.go @@ -107,7 +107,7 @@ type RunnerSet struct { type RunnerSetSpec struct { Replicas *int `json:"replicas"` - Template RunnerSpec `json:"template"` + Template RunnerTemplate `json:"template"` } type RunnerSetStatus struct { @@ -115,6 +115,12 @@ type RunnerSetStatus struct { ReadyReplicas int `json:"readyReplicas"` } +type RunnerTemplate struct { + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec RunnerSpec `json:"spec,omitempty"` +} + // +kubebuilder:object:root=true // RunnerList contains a list of Runner @@ -124,6 +130,42 @@ type RunnerSetList struct { Items []RunnerSet `json:"items"` } -func init() { - SchemeBuilder.Register(&Runner{}, &RunnerList{}, &RunnerSet{}, &RunnerSetList{}) +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:JSONPath=".spec.replicas",name=Desired,type=number +// +kubebuilder:printcolumn:JSONPath=".status.availableReplicas",name=Current,type=number +// +kubebuilder:printcolumn:JSONPath=".status.readyReplicas",name=Ready,type=number + +// RunnerSet is the Schema for the runnersets API +type RunnerDeployment struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec RunnerDeploymentSpec `json:"spec,omitempty"` + Status RunnerDeploymentStatus `json:"status,omitempty"` +} + +// RunnerSetSpec defines the desired state of RunnerDeployment +type RunnerDeploymentSpec struct { + Replicas *int `json:"replicas"` + + Template RunnerTemplate `json:"template"` +} + +type RunnerDeploymentStatus struct { + AvailableReplicas int `json:"availableReplicas"` + ReadyReplicas int `json:"readyReplicas"` +} + +// +kubebuilder:object:root=true + +// RunnerList contains a list of Runner +type RunnerDeploymentList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []RunnerDeployment `json:"items"` +} + +func init() { + SchemeBuilder.Register(&Runner{}, &RunnerList{}, &RunnerSet{}, &RunnerSetList{}, &RunnerDeployment{}, &RunnerDeploymentList{}) } diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index db9f191a..b584e180 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -52,6 +52,101 @@ func (in *Runner) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RunnerDeployment) DeepCopyInto(out *RunnerDeployment) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerDeployment. +func (in *RunnerDeployment) DeepCopy() *RunnerDeployment { + if in == nil { + return nil + } + out := new(RunnerDeployment) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *RunnerDeployment) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RunnerDeploymentList) DeepCopyInto(out *RunnerDeploymentList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]RunnerDeployment, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerDeploymentList. +func (in *RunnerDeploymentList) DeepCopy() *RunnerDeploymentList { + if in == nil { + return nil + } + out := new(RunnerDeploymentList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *RunnerDeploymentList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RunnerDeploymentSpec) DeepCopyInto(out *RunnerDeploymentSpec) { + *out = *in + if in.Replicas != nil { + in, out := &in.Replicas, &out.Replicas + *out = new(int) + **out = **in + } + in.Template.DeepCopyInto(&out.Template) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerDeploymentSpec. +func (in *RunnerDeploymentSpec) DeepCopy() *RunnerDeploymentSpec { + if in == nil { + return nil + } + out := new(RunnerDeploymentSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RunnerDeploymentStatus) DeepCopyInto(out *RunnerDeploymentStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerDeploymentStatus. +func (in *RunnerDeploymentStatus) DeepCopy() *RunnerDeploymentStatus { + if in == nil { + return nil + } + out := new(RunnerDeploymentStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RunnerList) DeepCopyInto(out *RunnerList) { *out = *in @@ -232,3 +327,20 @@ func (in *RunnerStatusRegistration) DeepCopy() *RunnerStatusRegistration { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RunnerTemplate) DeepCopyInto(out *RunnerTemplate) { + *out = *in + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunnerTemplate. +func (in *RunnerTemplate) DeepCopy() *RunnerTemplate { + if in == nil { + return nil + } + out := new(RunnerTemplate) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/bases/actions.summerwind.dev_runnerdeployments.yaml b/config/crd/bases/actions.summerwind.dev_runnerdeployments.yaml new file mode 100644 index 00000000..b5caf66f --- /dev/null +++ b/config/crd/bases/actions.summerwind.dev_runnerdeployments.yaml @@ -0,0 +1,199 @@ + +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.2.4 + creationTimestamp: null + name: runnerdeployments.actions.summerwind.dev +spec: + additionalPrinterColumns: + - JSONPath: .spec.replicas + name: Desired + type: number + - JSONPath: .status.availableReplicas + name: Current + type: number + - JSONPath: .status.readyReplicas + name: Ready + type: number + group: actions.summerwind.dev + names: + kind: RunnerDeployment + listKind: RunnerDeploymentList + plural: runnerdeployments + singular: runnerdeployment + scope: Namespaced + subresources: + status: {} + validation: + openAPIV3Schema: + description: RunnerSet is the Schema for the runnersets API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + 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/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + 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/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: RunnerSetSpec defines the desired state of RunnerDeployment + properties: + replicas: + type: integer + template: + properties: + metadata: + type: object + spec: + description: RunnerSpec defines the desired state of Runner + properties: + env: + items: + description: EnvVar represents an environment variable present + in a Container. + properties: + name: + description: Name of the environment variable. Must be + a C_IDENTIFIER. + type: string + value: + description: 'Variable references $(VAR_NAME) are expanded + using the previous defined environment variables in + the container and any service environment variables. + If a variable cannot be resolved, the reference in the + input string will be unchanged. The $(VAR_NAME) syntax + can be escaped with a double $$, ie: $$(VAR_NAME). Escaped + references will never be expanded, regardless of whether + the variable exists or not. Defaults to "".' + type: string + valueFrom: + description: Source for the environment variable's value. + Cannot be used if value is not empty. + properties: + configMapKeyRef: + description: Selects a key of a ConfigMap. + properties: + key: + description: The key to select. + type: string + name: + description: 'Name of the referent. More info: + https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, + uid?' + type: string + optional: + description: Specify whether the ConfigMap or + its key must be defined + type: boolean + required: + - key + type: object + fieldRef: + description: 'Selects a field of the pod: supports + metadata.name, metadata.namespace, metadata.labels, + metadata.annotations, spec.nodeName, spec.serviceAccountName, + status.hostIP, status.podIP.' + properties: + apiVersion: + description: Version of the schema the FieldPath + is written in terms of, defaults to "v1". + type: string + fieldPath: + description: Path of the field to select in the + specified API version. + type: string + required: + - fieldPath + type: object + resourceFieldRef: + description: 'Selects a resource of the container: + only resources limits and requests (limits.cpu, + limits.memory, limits.ephemeral-storage, requests.cpu, + requests.memory and requests.ephemeral-storage) + are currently supported.' + properties: + containerName: + description: 'Container name: required for volumes, + optional for env vars' + type: string + divisor: + description: Specifies the output format of the + exposed resources, defaults to "1" + type: string + resource: + description: 'Required: resource to select' + type: string + required: + - resource + type: object + secretKeyRef: + description: Selects a key of a secret in the pod's + namespace + properties: + key: + description: The key of the secret to select from. Must + be a valid secret key. + type: string + name: + description: 'Name of the referent. More info: + https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, + uid?' + type: string + optional: + description: Specify whether the Secret or its + key must be defined + type: boolean + required: + - key + type: object + type: object + required: + - name + type: object + type: array + image: + type: string + repository: + minLength: 3 + pattern: ^[^/]+/[^/]+$ + type: string + required: + - repository + type: object + type: object + required: + - replicas + - template + type: object + status: + properties: + availableReplicas: + type: integer + readyReplicas: + type: integer + required: + - availableReplicas + - readyReplicas + type: object + type: object + version: v1alpha1 + versions: + - name: v1alpha1 + served: true + storage: true +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/config/crd/bases/actions.summerwind.dev_runnersets.yaml b/config/crd/bases/actions.summerwind.dev_runnersets.yaml index f14232a0..5f5ddc7d 100644 --- a/config/crd/bases/actions.summerwind.dev_runnersets.yaml +++ b/config/crd/bases/actions.summerwind.dev_runnersets.yaml @@ -49,117 +49,127 @@ spec: replicas: type: integer template: - description: RunnerSpec defines the desired state of Runner properties: - env: - items: - description: EnvVar represents an environment variable present - in a Container. - properties: - name: - description: Name of the environment variable. Must be a C_IDENTIFIER. - type: string - value: - description: 'Variable references $(VAR_NAME) are expanded - using the previous defined environment variables in the - container and any service environment variables. If a variable - cannot be resolved, the reference in the input string will - be unchanged. The $(VAR_NAME) syntax can be escaped with - a double $$, ie: $$(VAR_NAME). Escaped references will never - be expanded, regardless of whether the variable exists or - not. Defaults to "".' - type: string - valueFrom: - description: Source for the environment variable's value. - Cannot be used if value is not empty. + metadata: + type: object + spec: + description: RunnerSpec defines the desired state of Runner + properties: + env: + items: + description: EnvVar represents an environment variable present + in a Container. properties: - configMapKeyRef: - description: Selects a key of a ConfigMap. + name: + description: Name of the environment variable. Must be + a C_IDENTIFIER. + type: string + value: + description: 'Variable references $(VAR_NAME) are expanded + using the previous defined environment variables in + the container and any service environment variables. + If a variable cannot be resolved, the reference in the + input string will be unchanged. The $(VAR_NAME) syntax + can be escaped with a double $$, ie: $$(VAR_NAME). Escaped + references will never be expanded, regardless of whether + the variable exists or not. Defaults to "".' + type: string + valueFrom: + description: Source for the environment variable's value. + Cannot be used if value is not empty. properties: - key: - description: The key to select. - type: string - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, - uid?' - type: string - optional: - description: Specify whether the ConfigMap or its - key must be defined - type: boolean - required: - - key - type: object - fieldRef: - description: 'Selects a field of the pod: supports metadata.name, - metadata.namespace, metadata.labels, metadata.annotations, - spec.nodeName, spec.serviceAccountName, status.hostIP, - status.podIP.' - properties: - apiVersion: - description: Version of the schema the FieldPath is - written in terms of, defaults to "v1". - type: string - fieldPath: - description: Path of the field to select in the specified - API version. - type: string - required: - - fieldPath - type: object - resourceFieldRef: - description: 'Selects a resource of the container: only - resources limits and requests (limits.cpu, limits.memory, - limits.ephemeral-storage, requests.cpu, requests.memory - and requests.ephemeral-storage) are currently supported.' - properties: - containerName: - description: 'Container name: required for volumes, - optional for env vars' - type: string - divisor: - description: Specifies the output format of the exposed - resources, defaults to "1" - type: string - resource: - description: 'Required: resource to select' - type: string - required: - - resource - type: object - secretKeyRef: - description: Selects a key of a secret in the pod's namespace - properties: - key: - description: The key of the secret to select from. Must - be a valid secret key. - type: string - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, - uid?' - type: string - optional: - description: Specify whether the Secret or its key - must be defined - type: boolean - required: - - key + configMapKeyRef: + description: Selects a key of a ConfigMap. + properties: + key: + description: The key to select. + type: string + name: + description: 'Name of the referent. More info: + https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, + uid?' + type: string + optional: + description: Specify whether the ConfigMap or + its key must be defined + type: boolean + required: + - key + type: object + fieldRef: + description: 'Selects a field of the pod: supports + metadata.name, metadata.namespace, metadata.labels, + metadata.annotations, spec.nodeName, spec.serviceAccountName, + status.hostIP, status.podIP.' + properties: + apiVersion: + description: Version of the schema the FieldPath + is written in terms of, defaults to "v1". + type: string + fieldPath: + description: Path of the field to select in the + specified API version. + type: string + required: + - fieldPath + type: object + resourceFieldRef: + description: 'Selects a resource of the container: + only resources limits and requests (limits.cpu, + limits.memory, limits.ephemeral-storage, requests.cpu, + requests.memory and requests.ephemeral-storage) + are currently supported.' + properties: + containerName: + description: 'Container name: required for volumes, + optional for env vars' + type: string + divisor: + description: Specifies the output format of the + exposed resources, defaults to "1" + type: string + resource: + description: 'Required: resource to select' + type: string + required: + - resource + type: object + secretKeyRef: + description: Selects a key of a secret in the pod's + namespace + properties: + key: + description: The key of the secret to select from. Must + be a valid secret key. + type: string + name: + description: 'Name of the referent. More info: + https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, + uid?' + type: string + optional: + description: Specify whether the Secret or its + key must be defined + type: boolean + required: + - key + type: object type: object + required: + - name type: object - required: - - name - type: object - type: array - image: - type: string - repository: - minLength: 3 - pattern: ^[^/]+/[^/]+$ - type: string - required: - - repository + type: array + image: + type: string + repository: + minLength: 3 + pattern: ^[^/]+/[^/]+$ + type: string + required: + - repository + type: object type: object required: - replicas diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 10ba6024..eba62d65 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -6,6 +6,26 @@ metadata: creationTimestamp: null name: manager-role rules: +- apiGroups: + - actions.summerwind.dev + resources: + - runnerdeployments + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - actions.summerwind.dev + resources: + - runnerdeployments/status + verbs: + - get + - patch + - update - apiGroups: - actions.summerwind.dev resources: diff --git a/controllers/runnerdeployment_controller.go b/controllers/runnerdeployment_controller.go new file mode 100644 index 00000000..3582caaa --- /dev/null +++ b/controllers/runnerdeployment_controller.go @@ -0,0 +1,260 @@ +/* +Copyright 2020 The actions-runner-controller 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 controllers + +import ( + "context" + "fmt" + "github.com/davecgh/go-spew/spew" + "github.com/go-logr/logr" + "hash/fnv" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/rand" + "k8s.io/client-go/tools/record" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sort" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/summerwind/actions-runner-controller/api/v1alpha1" +) + +const ( + LabelKeyRunnerTemplateHash = "runner-template-hash" + + runnerSetOwnerKey = ".metadata.controller" +) + +// RunnerDeploymentReconciler reconciles a Runner object +type RunnerDeploymentReconciler struct { + client.Client + Log logr.Logger + Recorder record.EventRecorder + Scheme *runtime.Scheme +} + +// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments/status,verbs=get;update;patch +// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnersets,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnersets/status,verbs=get;update;patch + +func (r *RunnerDeploymentReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { + ctx := context.Background() + log := r.Log.WithValues("runnerset", req.NamespacedName) + + var rd v1alpha1.RunnerDeployment + if err := r.Get(ctx, req.NamespacedName, &rd); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + if !rd.ObjectMeta.DeletionTimestamp.IsZero() { + return ctrl.Result{}, nil + } + + var myRunnerSetList v1alpha1.RunnerSetList + if err := r.List(ctx, &myRunnerSetList, client.InNamespace(req.Namespace), client.MatchingFields{runnerSetOwnerKey: req.Name}); err != nil { + return ctrl.Result{}, err + } + + myRunnerSets := myRunnerSetList.Items + + sort.Slice(myRunnerSets, func(i, j int) bool { + return myRunnerSets[i].GetCreationTimestamp().After(myRunnerSets[j].GetCreationTimestamp().Time) + }) + + var newestSet *v1alpha1.RunnerSet + + var oldSets []v1alpha1.RunnerSet + + if len(myRunnerSets) > 0 { + newestSet = &myRunnerSets[0] + } + + if len(myRunnerSets) > 1 { + oldSets = myRunnerSets[1:] + } + + desiredRS, err := r.newRunnerSet(rd) + if err != nil { + log.Error(err, "Could not create runnerset") + + return ctrl.Result{}, err + } + + if newestSet == nil { + if err := r.Client.Create(ctx, &desiredRS); err != nil { + log.Error(err, "Failed to create runnerset resource") + + return ctrl.Result{}, err + } + + return ctrl.Result{}, nil + } + + newestTemplateHash, ok := getTemplateHash(newestSet) + if !ok { + log.Info("Failed to get template hash of newest runnerset resource. It must be in an invalid state. Please manually delete the runnerset so that it is recreated") + + return ctrl.Result{}, nil + } + + desiredTemplateHash, ok := getTemplateHash(&desiredRS) + if !ok { + log.Info("Failed to get template hash of desired runnerset resource. It must be in an invalid state. Please manually delete the runnerset so that it is recreated") + + return ctrl.Result{}, nil + } + + if newestTemplateHash != desiredTemplateHash { + if err := r.Client.Create(ctx, &desiredRS); err != nil { + log.Error(err, "Failed to create runnerset resource") + + return ctrl.Result{}, err + } + + return ctrl.Result{}, nil + } + + // Please add more conditions that we can in-place update the newest runnerset without disruption + if newestSet.Spec.Replicas != desiredRS.Spec.Replicas { + newestSet.Spec.Replicas = desiredRS.Spec.Replicas + + if err := r.Client.Update(ctx, newestSet); err != nil { + log.Error(err, "Failed to update runnerset resource") + + return ctrl.Result{}, err + } + + return ctrl.Result{}, nil + } + + for i := range oldSets { + rs := oldSets[i] + + if err := r.Client.Delete(ctx, &rs); err != nil { + log.Error(err, "Failed to delete runner resource") + + return ctrl.Result{}, err + } + + r.Recorder.Event(&rd, corev1.EventTypeNormal, "RunnerSetDeleted", fmt.Sprintf("Deleted runnerset '%s'", rs.Name)) + log.Info("Deleted runnerset", "runnerdeployment", rd.ObjectMeta.Name, "runnerset", rs.Name) + } + + return ctrl.Result{}, nil +} + +func getTemplateHash(rs *v1alpha1.RunnerSet) (string, bool) { + hash, ok := rs.Labels[LabelKeyRunnerTemplateHash] + + return hash, ok +} + +// ComputeHash returns a hash value calculated from pod template and +// a collisionCount to avoid hash collision. The hash will be safe encoded to +// avoid bad words. +// +// Proudly modified and adopted from k8s.io/kubernetes/pkg/util/hash.DeepHashObject and +// k8s.io/kubernetes/pkg/controller.ComputeHash. +func ComputeHash(template interface{}) string { + hasher := fnv.New32a() + + hasher.Reset() + + printer := spew.ConfigState{ + Indent: " ", + SortKeys: true, + DisableMethods: true, + SpewKeys: true, + } + printer.Fprintf(hasher, "%#v", template) + + return rand.SafeEncodeString(fmt.Sprint(hasher.Sum32())) +} + +// Clones the given map and returns a new map with the given key and value added. +// Returns the given map, if labelKey is empty. +// +// Proudly copied from k8s.io/kubernetes/pkg/util/labels.CloneAndAddLabel +func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map[string]string { + if labelKey == "" { + // Don't need to add a label. + return labels + } + // Clone. + newLabels := map[string]string{} + for key, value := range labels { + newLabels[key] = value + } + newLabels[labelKey] = labelValue + return newLabels +} + +func (r *RunnerDeploymentReconciler) newRunnerSet(rd v1alpha1.RunnerDeployment) (v1alpha1.RunnerSet, error) { + newRSTemplate := *rd.Spec.Template.DeepCopy() + templateHash := ComputeHash(&newRSTemplate) + // Add template hash label to selector. + labels := CloneAndAddLabel(rd.Spec.Template.Labels, LabelKeyRunnerTemplateHash, templateHash) + + newRSTemplate.Labels = labels + + rs := v1alpha1.RunnerSet{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{ + GenerateName: rd.ObjectMeta.Name, + Namespace: rd.ObjectMeta.Namespace, + Labels: labels, + }, + Spec: v1alpha1.RunnerSetSpec{ + Replicas: rd.Spec.Replicas, + Template: newRSTemplate, + }, + } + + if err := ctrl.SetControllerReference(&rd, &rs, r.Scheme); err != nil { + return rs, err + } + + return rs, nil +} + +func (r *RunnerDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error { + r.Recorder = mgr.GetEventRecorderFor("runnerdeployment-controller") + + if err := mgr.GetFieldIndexer().IndexField(&v1alpha1.RunnerSet{}, runnerSetOwnerKey, func(rawObj runtime.Object) []string { + runnerSet := rawObj.(*v1alpha1.RunnerSet) + owner := metav1.GetControllerOf(runnerSet) + if owner == nil { + return nil + } + + if owner.APIVersion != v1alpha1.GroupVersion.String() || owner.Kind != "RunnerSet" { + return nil + } + + return []string{owner.Name} + }); err != nil { + return err + } + + return ctrl.NewControllerManagedBy(mgr). + For(&v1alpha1.RunnerDeployment{}). + Owns(&v1alpha1.RunnerSet{}). + Complete(r) +} diff --git a/controllers/runnerdeployment_controller_test.go b/controllers/runnerdeployment_controller_test.go new file mode 100644 index 00000000..2be57eb6 --- /dev/null +++ b/controllers/runnerdeployment_controller_test.go @@ -0,0 +1,175 @@ +package controllers + +import ( + "context" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/kubernetes/scheme" + ctrl "sigs.k8s.io/controller-runtime" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + + actionsv1alpha1 "github.com/summerwind/actions-runner-controller/api/v1alpha1" +) + +// SetupDeploymentTest will set up a testing environment. +// This includes: +// * creating a Namespace to be used during the test +// * starting the 'RunnerDeploymentReconciler' +// * stopping the 'RunnerDeploymentReconciler" after the test ends +// Call this function at the start of each of your tests. +func SetupDeploymentTest(ctx context.Context) *corev1.Namespace { + var stopCh chan struct{} + ns := &corev1.Namespace{} + + BeforeEach(func() { + stopCh = make(chan struct{}) + *ns = corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: "testns-" + randStringRunes(5)}, + } + + err := k8sClient.Create(ctx, ns) + Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") + + mgr, err := ctrl.NewManager(cfg, ctrl.Options{}) + Expect(err).NotTo(HaveOccurred(), "failed to create manager") + + controller := &RunnerDeploymentReconciler{ + Client: mgr.GetClient(), + Scheme: scheme.Scheme, + Log: logf.Log, + Recorder: mgr.GetEventRecorderFor("runnerset-controller"), + } + err = controller.SetupWithManager(mgr) + Expect(err).NotTo(HaveOccurred(), "failed to setup controller") + + go func() { + defer GinkgoRecover() + + err := mgr.Start(stopCh) + Expect(err).NotTo(HaveOccurred(), "failed to start manager") + }() + }) + + AfterEach(func() { + close(stopCh) + + err := k8sClient.Delete(ctx, ns) + Expect(err).NotTo(HaveOccurred(), "failed to delete test namespace") + }) + + return ns +} + +var _ = Context("Inside of a new namespace", func() { + ctx := context.TODO() + ns := SetupDeploymentTest(ctx) + + Describe("when no existing resources exist", func() { + + It("should create a new RunnerSet resource from the specified template, add a another RunnerSet on template modification, and eventually removes old runnersets", func() { + name := "example-runnerdeploy" + + { + rs := &actionsv1alpha1.RunnerDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: ns.Name, + }, + Spec: actionsv1alpha1.RunnerDeploymentSpec{ + Replicas: intPtr(1), + Template: actionsv1alpha1.RunnerTemplate{ + Spec: actionsv1alpha1.RunnerSpec{ + Repository: "foo/bar", + Image: "bar", + Env: []corev1.EnvVar{ + {Name: "FOO", Value: "FOOVALUE"}, + }, + }, + }, + }, + } + + err := k8sClient.Create(ctx, rs) + + Expect(err).NotTo(HaveOccurred(), "failed to create test RunnerSet resource") + + runnerSets := actionsv1alpha1.RunnerSetList{Items: []actionsv1alpha1.RunnerSet{}} + + Eventually( + func() int { + err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns.Name)) + if err != nil { + logf.Log.Error(err, "list runner sets") + } + + return len(runnerSets.Items) + }, + time.Second*5, time.Millisecond*500).Should(BeEquivalentTo(1)) + + Eventually( + func() int { + err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns.Name)) + if err != nil { + logf.Log.Error(err, "list runner sets") + } + + if len(runnerSets.Items) == 0 { + logf.Log.Info("No runnersets exist yet") + return -1 + } + + return *runnerSets.Items[0].Spec.Replicas + }, + time.Second*5, time.Millisecond*500).Should(BeEquivalentTo(1)) + } + + { + // We wrap the update in the Eventually block to avoid the below error that occurs due to concurrent modification + // made by the controller to update .Status.AvailableReplicas and .Status.ReadyReplicas + // Operation cannot be fulfilled on runnersets.actions.summerwind.dev "example-runnerset": the object has been modified; please apply your changes to the latest version and try again + Eventually(func() error { + var rd actionsv1alpha1.RunnerDeployment + + err := k8sClient.Get(ctx, types.NamespacedName{Namespace: ns.Name, Name: name}, &rd) + + Expect(err).NotTo(HaveOccurred(), "failed to get test RunnerSet resource") + + rd.Spec.Replicas = intPtr(2) + + return k8sClient.Update(ctx, &rd) + }, + time.Second*1, time.Millisecond*500).Should(BeNil()) + + runnerSets := actionsv1alpha1.RunnerSetList{Items: []actionsv1alpha1.RunnerSet{}} + + Eventually( + func() int { + err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns.Name)) + if err != nil { + logf.Log.Error(err, "list runner sets") + } + + return len(runnerSets.Items) + }, + time.Second*5, time.Millisecond*500).Should(BeEquivalentTo(1)) + + Eventually( + func() int { + err := k8sClient.List(ctx, &runnerSets, client.InNamespace(ns.Name)) + if err != nil { + logf.Log.Error(err, "list runner sets") + } + + return *runnerSets.Items[0].Spec.Replicas + }, + time.Second*5, time.Millisecond*500).Should(BeEquivalentTo(2)) + } + }) + }) +}) diff --git a/controllers/runnerset_controller.go b/controllers/runnerset_controller.go index bb745379..5dcbb974 100644 --- a/controllers/runnerset_controller.go +++ b/controllers/runnerset_controller.go @@ -138,13 +138,15 @@ func (r *RunnerSetReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { } func (r *RunnerSetReconciler) newRunner(rs v1alpha1.RunnerSet) (v1alpha1.Runner, error) { + objectMeta := rs.Spec.Template.ObjectMeta.DeepCopy() + + objectMeta.GenerateName = rs.ObjectMeta.Name + objectMeta.Namespace = rs.ObjectMeta.Namespace + runner := v1alpha1.Runner{ - TypeMeta: metav1.TypeMeta{}, - ObjectMeta: metav1.ObjectMeta{ - GenerateName: rs.ObjectMeta.Name, - Namespace: rs.ObjectMeta.Namespace, - }, - Spec: rs.Spec.Template, + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: *objectMeta, + Spec: rs.Spec.Template.Spec, } if err := ctrl.SetControllerReference(&rs, &runner, r.Scheme); err != nil { diff --git a/controllers/runnerset_controller_test.go b/controllers/runnerset_controller_test.go index 1fafb570..593181c0 100644 --- a/controllers/runnerset_controller_test.go +++ b/controllers/runnerset_controller_test.go @@ -98,11 +98,13 @@ var _ = Context("Inside of a new namespace", func() { }, Spec: actionsv1alpha1.RunnerSetSpec{ Replicas: intPtr(1), - Template: actionsv1alpha1.RunnerSpec{ - Repository: "foo/bar", - Image: "bar", - Env: []corev1.EnvVar{ - {Name: "FOO", Value: "FOOVALUE"}, + Template: actionsv1alpha1.RunnerTemplate{ + Spec: actionsv1alpha1.RunnerSpec{ + Repository: "foo/bar", + Image: "bar", + Env: []corev1.EnvVar{ + {Name: "FOO", Value: "FOOVALUE"}, + }, }, }, }, diff --git a/go.mod b/go.mod index 3b247669..592121d4 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect github.com/bradleyfalzon/ghinstallation v1.1.1 + github.com/davecgh/go-spew v1.1.1 github.com/go-logr/logr v0.1.0 github.com/google/go-github v17.0.0+incompatible github.com/google/go-github/v29 v29.0.2 diff --git a/main.go b/main.go index 49f9daf6..bce94136 100644 --- a/main.go +++ b/main.go @@ -121,6 +121,17 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "RunnerSet") os.Exit(1) } + + runnerDeploymentReconciler := &controllers.RunnerDeploymentReconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("RunnerDeployment"), + Scheme: mgr.GetScheme(), + } + + if err = runnerDeploymentReconciler.SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "RunnerDeployment") + os.Exit(1) + } // +kubebuilder:scaffold:builder setupLog.Info("starting manager")