2020-02-26 21:23:23 +09:00
/ *
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"
2020-03-15 18:08:11 +09:00
"hash/fnv"
2021-03-05 10:15:39 +09:00
"reflect"
2020-03-15 18:08:11 +09:00
"sort"
2020-04-02 09:51:40 +09:00
"time"
2020-03-15 18:08:11 +09:00
2020-06-27 17:26:46 +09:00
"k8s.io/apimachinery/pkg/types"
2020-02-26 21:23:23 +09:00
"github.com/davecgh/go-spew/spew"
"github.com/go-logr/logr"
"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"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021-06-22 17:55:06 +09:00
"github.com/actions-runner-controller/actions-runner-controller/api/v1alpha1"
"github.com/actions-runner-controller/actions-runner-controller/controllers/metrics"
2020-02-26 21:23:23 +09:00
)
const (
2021-03-11 20:16:36 +09:00
LabelKeyRunnerTemplateHash = "runner-template-hash"
LabelKeyRunnerDeploymentName = "runner-deployment-name"
2020-03-03 10:45:39 +09:00
runnerSetOwnerKey = ".metadata.controller"
2020-02-26 21:23:23 +09:00
)
// RunnerDeploymentReconciler reconciles a Runner object
type RunnerDeploymentReconciler struct {
client . Client
2021-02-18 20:19:08 +09:00
Log logr . Logger
Recorder record . EventRecorder
Scheme * runtime . Scheme
CommonRunnerLabels [ ] string
2021-02-19 10:33:04 +09:00
Name string
2020-02-26 21:23:23 +09:00
}
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments,verbs=get;list;watch;create;update;patch;delete
2020-10-06 09:23:03 +09:00
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments/finalizers,verbs=get;list;watch;create;update;patch;delete
2020-02-26 21:23:23 +09:00
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerdeployments/status,verbs=get;update;patch
2020-03-15 18:08:11 +09:00
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerreplicasets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=actions.summerwind.dev,resources=runnerreplicasets/status,verbs=get;update;patch
2020-03-27 23:25:37 +09:00
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
2020-02-26 21:23:23 +09:00
2021-06-22 17:10:09 +09:00
func ( r * RunnerDeploymentReconciler ) Reconcile ( ctx context . Context , req ctrl . Request ) ( ctrl . Result , error ) {
2020-04-02 09:51:40 +09:00
log := r . Log . WithValues ( "runnerdeployment" , req . NamespacedName )
2020-02-26 21:23:23 +09:00
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
}
2021-03-19 16:14:02 +09:00
metrics . SetRunnerDeployment ( rd )
2020-03-10 09:14:11 +09:00
var myRunnerReplicaSetList v1alpha1 . RunnerReplicaSetList
if err := r . List ( ctx , & myRunnerReplicaSetList , client . InNamespace ( req . Namespace ) , client . MatchingFields { runnerSetOwnerKey : req . Name } ) ; err != nil {
2020-03-03 10:50:52 +09:00
return ctrl . Result { } , err
2020-02-26 21:23:23 +09:00
}
2020-03-10 09:14:11 +09:00
myRunnerReplicaSets := myRunnerReplicaSetList . Items
2020-02-26 21:23:23 +09:00
2020-03-10 09:14:11 +09:00
sort . Slice ( myRunnerReplicaSets , func ( i , j int ) bool {
return myRunnerReplicaSets [ i ] . GetCreationTimestamp ( ) . After ( myRunnerReplicaSets [ j ] . GetCreationTimestamp ( ) . Time )
2020-02-26 21:23:23 +09:00
} )
2020-03-10 09:14:11 +09:00
var newestSet * v1alpha1 . RunnerReplicaSet
2020-02-26 21:23:23 +09:00
2020-03-10 09:14:11 +09:00
var oldSets [ ] v1alpha1 . RunnerReplicaSet
2020-02-26 21:23:23 +09:00
2020-03-10 09:14:11 +09:00
if len ( myRunnerReplicaSets ) > 0 {
newestSet = & myRunnerReplicaSets [ 0 ]
2020-02-26 21:23:23 +09:00
}
2020-03-10 09:14:11 +09:00
if len ( myRunnerReplicaSets ) > 1 {
oldSets = myRunnerReplicaSets [ 1 : ]
2020-02-26 21:23:23 +09:00
}
2020-07-19 18:42:06 +09:00
desiredRS , err := r . newRunnerReplicaSet ( rd )
2020-02-26 21:23:23 +09:00
if err != nil {
2020-07-03 09:05:46 +09:00
r . Recorder . Event ( & rd , corev1 . EventTypeNormal , "RunnerAutoscalingFailure" , err . Error ( ) )
2020-06-27 17:26:46 +09:00
2020-03-15 18:08:11 +09:00
log . Error ( err , "Could not create runnerreplicaset" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , err
}
if newestSet == nil {
2020-06-27 17:26:46 +09:00
if err := r . Client . Create ( ctx , desiredRS ) ; err != nil {
2020-03-15 18:08:11 +09:00
log . Error ( err , "Failed to create runnerreplicaset resource" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , err
}
2022-03-13 12:25:53 +00:00
log . Info ( "Created runnerreplicaset" , "runnerreplicaset" , desiredRS . Name )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , nil
}
newestTemplateHash , ok := getTemplateHash ( newestSet )
if ! ok {
2020-03-15 18:08:11 +09:00
log . Info ( "Failed to get template hash of newest runnerreplicaset resource. It must be in an invalid state. Please manually delete the runnerreplicaset so that it is recreated" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , nil
}
2020-06-27 17:26:46 +09:00
desiredTemplateHash , ok := getTemplateHash ( desiredRS )
2020-02-26 21:23:23 +09:00
if ! ok {
2020-03-15 18:08:11 +09:00
log . Info ( "Failed to get template hash of desired runnerreplicaset resource. It must be in an invalid state. Please manually delete the runnerreplicaset so that it is recreated" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , nil
}
if newestTemplateHash != desiredTemplateHash {
2020-06-27 17:26:46 +09:00
if err := r . Client . Create ( ctx , desiredRS ) ; err != nil {
2020-03-15 18:08:11 +09:00
log . Error ( err , "Failed to create runnerreplicaset resource" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , err
}
2022-03-13 12:25:53 +00:00
log . Info ( "Created runnerreplicaset" , "runnerreplicaset" , desiredRS . Name )
2020-04-02 09:51:40 +09:00
// We requeue in order to clean up old runner replica sets later.
// Otherwise, they aren't cleaned up until the next re-sync interval.
return ctrl . Result { RequeueAfter : 5 * time . Second } , nil
2020-02-26 21:23:23 +09:00
}
2021-03-05 10:15:39 +09:00
if ! reflect . DeepEqual ( newestSet . Spec . Selector , desiredRS . Spec . Selector ) {
updateSet := newestSet . DeepCopy ( )
updateSet . Spec = * desiredRS . Spec . DeepCopy ( )
// A selector update change doesn't trigger replicaset replacement,
// but we still need to update the existing replicaset with it.
// Otherwise selector-based runner query will never work on replicasets created before the controller v0.17.0
2021-06-22 17:55:06 +09:00
// See https://github.com/actions-runner-controller/actions-runner-controller/pull/355#discussion_r585379259
2021-03-05 10:15:39 +09:00
if err := r . Client . Update ( ctx , updateSet ) ; err != nil {
log . Error ( err , "Failed to update runnerreplicaset resource" )
return ctrl . Result { } , err
}
2022-07-17 19:43:24 +09:00
log . V ( 1 ) . Info ( "Updated runnerreplicaset due to selector change" )
2021-03-05 10:15:39 +09:00
// At this point, we are already sure that there's no need to create a new replicaset
// as the runner template hash is not changed.
//
// But we still need to requeue for the (possibly rare) cases that there are still old replicasets that needs
// to be cleaned up.
return ctrl . Result { RequeueAfter : 5 * time . Second } , nil
}
2020-04-02 09:51:40 +09:00
const defaultReplicas = 1
currentDesiredReplicas := getIntOrDefault ( newestSet . Spec . Replicas , defaultReplicas )
newDesiredReplicas := getIntOrDefault ( desiredRS . Spec . Replicas , defaultReplicas )
2020-03-15 18:08:11 +09:00
// Please add more conditions that we can in-place update the newest runnerreplicaset without disruption
2022-06-28 13:50:07 +09:00
//
// If we missed taking the EffectiveTime diff into account, you might end up experiencing scale-ups being delayed scale-down.
// See https://github.com/actions-runner-controller/actions-runner-controller/pull/1477#issuecomment-1164154496
2022-07-17 19:43:24 +09:00
var et1 , et2 time . Time
if newestSet . Spec . EffectiveTime != nil {
et1 = newestSet . Spec . EffectiveTime . Time
}
if rd . Spec . EffectiveTime != nil {
et2 = rd . Spec . EffectiveTime . Time
}
if currentDesiredReplicas != newDesiredReplicas || et1 != et2 {
2020-04-02 09:51:40 +09:00
newestSet . Spec . Replicas = & newDesiredReplicas
2022-02-20 06:59:56 +00:00
newestSet . Spec . EffectiveTime = rd . Spec . EffectiveTime
2020-02-26 21:23:23 +09:00
if err := r . Client . Update ( ctx , newestSet ) ; err != nil {
2020-03-15 18:08:11 +09:00
log . Error ( err , "Failed to update runnerreplicaset resource" )
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , err
}
2022-07-17 19:43:24 +09:00
log . V ( 1 ) . Info ( "Updated runnerreplicaset due to spec change" ,
"currentDesiredReplicas" , currentDesiredReplicas ,
"newDesiredReplicas" , newDesiredReplicas ,
"currentEffectiveTime" , newestSet . Spec . EffectiveTime ,
"newEffectiveTime" , rd . Spec . EffectiveTime ,
)
2020-04-02 09:51:40 +09:00
return ctrl . Result { } , err
2020-02-26 21:23:23 +09:00
}
feat: Support for scaling from/to zero (#465)
This is an attempt to support scaling from/to zero.
The basic idea is that we create a one-off "registration-only" runner pod on RunnerReplicaSet being scaled to zero, so that there is one "offline" runner, which enables GitHub Actions to queue jobs instead of discarding those.
GitHub Actions seems to immediately throw away the new job when there are no runners at all. Generally, having runners of any status, `busy`, `idle`, or `offline` would prevent GitHub actions from failing jobs. But retaining `busy` or `idle` runners means that we need to keep runner pods running, which conflicts with our desired to scale to/from zero, hence we retain `offline` runners.
In this change, I enhanced the runnerreplicaset controller to create a registration-only runner on very beginning of its reconciliation logic, only when a runnerreplicaset is scaled to zero. The runner controller creates the registration-only runner pod, waits for it to become "offline", and then removes the runner pod. The runner on GitHub stays `offline`, until the runner resource on K8s is deleted. As we remove the registration-only runner pod as soon as it registers, this doesn't block cluster-autoscaler.
Related to #447
2021-05-02 16:11:36 +09:00
// Do we have old runner replica sets that should eventually deleted?
2020-04-02 09:51:40 +09:00
if len ( oldSets ) > 0 {
2021-05-21 09:10:47 +09:00
var readyReplicas int
if newestSet . Status . ReadyReplicas != nil {
readyReplicas = * newestSet . Status . ReadyReplicas
}
2020-02-26 21:23:23 +09:00
2021-03-20 07:34:25 +09:00
oldSetsCount := len ( oldSets )
logWithDebugInfo := log . WithValues (
"newest_runnerreplicaset" , types . NamespacedName {
2020-04-02 09:51:40 +09:00
Namespace : newestSet . Namespace ,
Name : newestSet . Name ,
2021-03-20 07:34:25 +09:00
} ,
"newest_runnerreplicaset_replicas_ready" , readyReplicas ,
"newest_runnerreplicaset_replicas_desired" , currentDesiredReplicas ,
"old_runnerreplicasets_count" , oldSetsCount ,
)
if readyReplicas < currentDesiredReplicas {
logWithDebugInfo .
Info ( "Waiting until the newest runnerreplicaset to be 100% available" )
return ctrl . Result { } , nil
}
2020-02-26 21:23:23 +09:00
2021-03-20 07:34:25 +09:00
if oldSetsCount > 0 {
logWithDebugInfo .
Info ( "The newest runnerreplicaset is 100% available. Deleting old runnerreplicasets" )
2020-02-26 21:23:23 +09:00
}
2020-04-02 09:51:40 +09:00
for i := range oldSets {
rs := oldSets [ i ]
2022-03-13 12:18:22 +00:00
rslog := log . WithValues ( "runnerreplicaset" , rs . Name )
if rs . Status . Replicas != nil && * rs . Status . Replicas > 0 {
if rs . Spec . Replicas != nil && * rs . Spec . Replicas == 0 {
rslog . V ( 2 ) . Info ( "Waiting for runnerreplicaset to scale to zero" )
continue
}
updated := rs . DeepCopy ( )
zero := 0
updated . Spec . Replicas = & zero
if err := r . Client . Update ( ctx , updated ) ; err != nil {
rslog . Error ( err , "Failed to scale runnerreplicaset to zero" )
return ctrl . Result { } , err
}
rslog . Info ( "Scaled runnerreplicaset to zero" )
continue
}
2020-04-02 09:51:40 +09:00
if err := r . Client . Delete ( ctx , & rs ) ; err != nil {
2022-03-13 12:18:22 +00:00
rslog . Error ( err , "Failed to delete runnerreplicaset resource" )
2020-04-02 09:51:40 +09:00
return ctrl . Result { } , err
}
r . Recorder . Event ( & rd , corev1 . EventTypeNormal , "RunnerReplicaSetDeleted" , fmt . Sprintf ( "Deleted runnerreplicaset '%s'" , rs . Name ) )
2022-03-13 12:18:22 +00:00
rslog . Info ( "Deleted runnerreplicaset" )
2020-04-02 09:51:40 +09:00
}
2020-02-26 21:23:23 +09:00
}
2021-05-21 09:10:47 +09:00
var replicaSets [ ] v1alpha1 . RunnerReplicaSet
2020-06-27 17:26:46 +09:00
2021-05-21 09:10:47 +09:00
replicaSets = append ( replicaSets , * newestSet )
replicaSets = append ( replicaSets , oldSets ... )
2020-06-27 17:26:46 +09:00
2021-05-21 09:10:47 +09:00
var totalCurrentReplicas , totalStatusAvailableReplicas , updatedReplicas int
for _ , rs := range replicaSets {
var current , available int
if rs . Status . Replicas != nil {
current = * rs . Status . Replicas
}
if rs . Status . AvailableReplicas != nil {
available = * rs . Status . AvailableReplicas
}
totalCurrentReplicas += current
totalStatusAvailableReplicas += available
}
if newestSet . Status . Replicas != nil {
updatedReplicas = * newestSet . Status . Replicas
}
var status v1alpha1 . RunnerDeploymentStatus
status . AvailableReplicas = & totalStatusAvailableReplicas
status . ReadyReplicas = & totalStatusAvailableReplicas
status . DesiredReplicas = & newDesiredReplicas
status . Replicas = & totalCurrentReplicas
status . UpdatedReplicas = & updatedReplicas
if ! reflect . DeepEqual ( rd . Status , status ) {
updated := rd . DeepCopy ( )
updated . Status = status
if err := r . Status ( ) . Patch ( ctx , updated , client . MergeFrom ( & rd ) ) ; err != nil {
log . Info ( "Failed to patch runnerdeployment status. Retrying immediately" , "error" , err . Error ( ) )
return ctrl . Result {
Requeue : true ,
} , nil
2020-06-27 17:26:46 +09:00
}
}
2020-02-26 21:23:23 +09:00
return ctrl . Result { } , nil
}
2020-04-02 09:51:40 +09:00
func getIntOrDefault ( p * int , d int ) int {
if p == nil {
return d
}
return * p
}
2020-03-10 09:14:11 +09:00
func getTemplateHash ( rs * v1alpha1 . RunnerReplicaSet ) ( string , bool ) {
2020-02-26 21:23:23 +09:00
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
}
2021-03-05 10:15:39 +09:00
// Clones the given selector and returns a new selector with the given key and value added.
// Returns the given selector, if labelKey is empty.
//
// Proudly copied from k8s.io/kubernetes/pkg/util/labels.CloneSelectorAndAddLabel
func CloneSelectorAndAddLabel ( selector * metav1 . LabelSelector , labelKey , labelValue string ) * metav1 . LabelSelector {
if labelKey == "" {
// Don't need to add a label.
return selector
}
// Clone.
newSelector := new ( metav1 . LabelSelector )
newSelector . MatchLabels = make ( map [ string ] string )
if selector . MatchLabels != nil {
for key , val := range selector . MatchLabels {
newSelector . MatchLabels [ key ] = val
}
}
newSelector . MatchLabels [ labelKey ] = labelValue
if selector . MatchExpressions != nil {
newMExps := make ( [ ] metav1 . LabelSelectorRequirement , len ( selector . MatchExpressions ) )
for i , me := range selector . MatchExpressions {
newMExps [ i ] . Key = me . Key
newMExps [ i ] . Operator = me . Operator
if me . Values != nil {
newMExps [ i ] . Values = make ( [ ] string , len ( me . Values ) )
copy ( newMExps [ i ] . Values , me . Values )
} else {
newMExps [ i ] . Values = nil
}
}
newSelector . MatchExpressions = newMExps
} else {
newSelector . MatchExpressions = nil
}
return newSelector
}
2020-07-19 18:42:06 +09:00
func ( r * RunnerDeploymentReconciler ) newRunnerReplicaSet ( rd v1alpha1 . RunnerDeployment ) ( * v1alpha1 . RunnerReplicaSet , error ) {
2021-03-05 10:15:39 +09:00
return newRunnerReplicaSet ( & rd , r . CommonRunnerLabels , r . Scheme )
}
2021-03-11 20:16:36 +09:00
func getSelector ( rd * v1alpha1 . RunnerDeployment ) * metav1 . LabelSelector {
selector := rd . Spec . Selector
if selector == nil {
selector = & metav1 . LabelSelector { MatchLabels : map [ string ] string { LabelKeyRunnerDeploymentName : rd . Name } }
}
return selector
}
2021-03-05 10:15:39 +09:00
func newRunnerReplicaSet ( rd * v1alpha1 . RunnerDeployment , commonRunnerLabels [ ] string , scheme * runtime . Scheme ) ( * v1alpha1 . RunnerReplicaSet , error ) {
2020-02-26 21:23:23 +09:00
newRSTemplate := * rd . Spec . Template . DeepCopy ( )
2021-03-05 10:15:39 +09:00
2022-05-16 02:38:32 -07:00
newRSTemplate . Spec . Labels = append ( newRSTemplate . Spec . Labels , commonRunnerLabels ... )
2021-02-18 20:19:08 +09:00
2021-03-11 20:16:36 +09:00
templateHash := ComputeHash ( & newRSTemplate )
// Add template hash label to selector.
newRSTemplate . ObjectMeta . Labels = CloneAndAddLabel ( newRSTemplate . ObjectMeta . Labels , LabelKeyRunnerTemplateHash , templateHash )
// This label selector is used by default when rd.Spec.Selector is empty.
newRSTemplate . ObjectMeta . Labels = CloneAndAddLabel ( newRSTemplate . ObjectMeta . Labels , LabelKeyRunnerDeploymentName , rd . Name )
selector := getSelector ( rd )
2020-02-26 21:23:23 +09:00
2021-03-05 10:15:39 +09:00
newRSSelector := CloneSelectorAndAddLabel ( selector , LabelKeyRunnerTemplateHash , templateHash )
2020-03-10 09:14:11 +09:00
rs := v1alpha1 . RunnerReplicaSet {
2020-02-26 21:23:23 +09:00
TypeMeta : metav1 . TypeMeta { } ,
ObjectMeta : metav1 . ObjectMeta {
2020-03-15 21:50:45 +09:00
GenerateName : rd . ObjectMeta . Name + "-" ,
2020-02-26 21:23:23 +09:00
Namespace : rd . ObjectMeta . Namespace ,
2021-03-11 20:16:36 +09:00
Labels : newRSTemplate . ObjectMeta . Labels ,
2020-02-26 21:23:23 +09:00
} ,
2020-03-10 09:14:11 +09:00
Spec : v1alpha1 . RunnerReplicaSetSpec {
2022-02-20 06:59:56 +00:00
Replicas : rd . Spec . Replicas ,
Selector : newRSSelector ,
Template : newRSTemplate ,
EffectiveTime : rd . Spec . EffectiveTime ,
2020-02-26 21:23:23 +09:00
} ,
}
2021-03-05 10:15:39 +09:00
if err := ctrl . SetControllerReference ( rd , & rs , scheme ) ; err != nil {
2020-06-27 17:26:46 +09:00
return & rs , err
2020-02-26 21:23:23 +09:00
}
2020-06-27 17:26:46 +09:00
return & rs , nil
2020-02-26 21:23:23 +09:00
}
func ( r * RunnerDeploymentReconciler ) SetupWithManager ( mgr ctrl . Manager ) error {
2021-02-16 18:51:33 +09:00
name := "runnerdeployment-controller"
2021-02-19 10:33:04 +09:00
if r . Name != "" {
name = r . Name
}
2021-02-16 18:51:33 +09:00
r . Recorder = mgr . GetEventRecorderFor ( name )
2020-02-26 21:23:23 +09:00
2021-06-22 17:10:09 +09:00
if err := mgr . GetFieldIndexer ( ) . IndexField ( context . TODO ( ) , & v1alpha1 . RunnerReplicaSet { } , runnerSetOwnerKey , func ( rawObj client . Object ) [ ] string {
2020-03-10 09:14:11 +09:00
runnerSet := rawObj . ( * v1alpha1 . RunnerReplicaSet )
2020-03-03 10:45:39 +09:00
owner := metav1 . GetControllerOf ( runnerSet )
if owner == nil {
return nil
}
2020-03-06 08:53:28 +09:00
if owner . APIVersion != v1alpha1 . GroupVersion . String ( ) || owner . Kind != "RunnerDeployment" {
2020-03-03 10:45:39 +09:00
return nil
}
return [ ] string { owner . Name }
} ) ; err != nil {
return err
}
2020-02-26 21:23:23 +09:00
return ctrl . NewControllerManagedBy ( mgr ) .
For ( & v1alpha1 . RunnerDeployment { } ) .
2020-03-10 09:14:11 +09:00
Owns ( & v1alpha1 . RunnerReplicaSet { } ) .
2021-02-16 18:51:33 +09:00
Named ( name ) .
2020-02-26 21:23:23 +09:00
Complete ( r )
}