github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/controller/lifecycle.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package controller
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/klog/v2"
    23  
    24  	katalystbase "github.com/kubewharf/katalyst-core/cmd/base"
    25  	"github.com/kubewharf/katalyst-core/pkg/config"
    26  	"github.com/kubewharf/katalyst-core/pkg/controller/lifecycle"
    27  	agent_healthz "github.com/kubewharf/katalyst-core/pkg/controller/lifecycle/agent-healthz"
    28  )
    29  
    30  const (
    31  	LifeCycleControllerName = "lifecycle"
    32  )
    33  
    34  func StartLifeCycleController(ctx context.Context, controlCtx *katalystbase.GenericContext,
    35  	conf *config.Configuration, _ interface{}, _ string,
    36  ) (bool, error) {
    37  	var (
    38  		cnrLifecycle *lifecycle.CNRLifecycle
    39  		cncLifecycle *lifecycle.CNCLifecycle
    40  		eviction     *agent_healthz.HealthzController
    41  		err          error
    42  	)
    43  
    44  	cnrLifecycle, err = lifecycle.NewCNRLifecycle(ctx,
    45  		conf.GenericConfiguration,
    46  		conf.GenericControllerConfiguration,
    47  		conf.ControllersConfiguration.CNRLifecycleConfig,
    48  		controlCtx.Client,
    49  		controlCtx.KubeInformerFactory.Core().V1().Nodes(),
    50  		controlCtx.InternalInformerFactory.Node().V1alpha1().CustomNodeResources(),
    51  		controlCtx.EmitterPool.GetDefaultMetricsEmitter(),
    52  	)
    53  	if err != nil {
    54  		klog.Errorf("failed to new CNR lifecycle controller")
    55  		return false, err
    56  	}
    57  
    58  	if conf.LifeCycleConfig.EnableCNCLifecycle {
    59  		cncLifecycle, err = lifecycle.NewCNCLifecycle(ctx,
    60  			conf.GenericConfiguration,
    61  			conf.GenericControllerConfiguration,
    62  			conf.ControllersConfiguration.CNCLifecycleConfig,
    63  			controlCtx.Client,
    64  			controlCtx.KubeInformerFactory.Core().V1().Nodes(),
    65  			controlCtx.InternalInformerFactory.Config().V1alpha1().CustomNodeConfigs(),
    66  			controlCtx.EmitterPool.GetDefaultMetricsEmitter(),
    67  		)
    68  		if err != nil {
    69  			klog.Errorf("failed to new CNC lifecycle controller")
    70  			return false, err
    71  		}
    72  	}
    73  
    74  	if conf.LifeCycleConfig.EnableHealthz {
    75  		eviction, err = agent_healthz.NewHealthzController(ctx,
    76  			conf.GenericConfiguration,
    77  			conf.GenericControllerConfiguration,
    78  			conf.ControllersConfiguration.LifeCycleConfig,
    79  			controlCtx.Client,
    80  			controlCtx.KubeInformerFactory.Core().V1().Nodes(),
    81  			controlCtx.KubeInformerFactory.Core().V1().Pods(),
    82  			controlCtx.InternalInformerFactory.Node().V1alpha1().CustomNodeResources(),
    83  			controlCtx.EmitterPool.GetDefaultMetricsEmitter(),
    84  		)
    85  		if err != nil {
    86  			klog.Errorf("failed to new eviction controller")
    87  			return false, err
    88  		}
    89  	}
    90  
    91  	if cnrLifecycle != nil {
    92  		go cnrLifecycle.Run()
    93  	}
    94  
    95  	if cncLifecycle != nil {
    96  		go cncLifecycle.Run()
    97  	}
    98  
    99  	if eviction != nil {
   100  		go eviction.Run()
   101  	}
   102  
   103  	return true, nil
   104  }