github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/rsm/types.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package rsm
    21  
    22  import (
    23  	"context"
    24  
    25  	"github.com/go-logr/logr"
    26  	"k8s.io/client-go/tools/record"
    27  
    28  	workloads "github.com/1aal/kubeblocks/apis/workloads/v1alpha1"
    29  	roclient "github.com/1aal/kubeblocks/pkg/controller/client"
    30  	"github.com/1aal/kubeblocks/pkg/controller/graph"
    31  )
    32  
    33  const (
    34  	// FeatureGateRSMCompatibilityMode whether run rsm in compatibility mode(i.e. make rsm having backward and forward compatibility with KubeBlocks version prior 0.6.0)
    35  	// what the compatibility mode will do:
    36  	// 1. labels compatibility
    37  	//    copy labels of rsm to secondary resources(i.e. svc, sts, cm) in creation; list secondary resources by labels of rsm in update and deletion
    38  	// 2. owner reference compatibility
    39  	//    copy the owner ref of rsm to secondary resources; list secondary resources by owner ref of rsm in update and deletion
    40  	// 3. finalizer compatibility
    41  	//    copy the finalizer of rsm to secondary resources; remove the finalizer same as rsm in deletion
    42  	// what if not:
    43  	// labels, owner ref and finalizer of secondary resources will be generated by (not copied from) rsm.
    44  	FeatureGateRSMCompatibilityMode = "RSM_COMPATIBILITY_MODE"
    45  
    46  	workloadsManagedByLabelKey = "workloads.kubeblocks.io/managed-by"
    47  	workloadsInstanceLabelKey  = "workloads.kubeblocks.io/instance"
    48  
    49  	kindReplicatedStateMachine = "ReplicatedStateMachine"
    50  
    51  	roleLabelKey          = "kubeblocks.io/role"
    52  	rsmAccessModeLabelKey = "rsm.workloads.kubeblocks.io/access-mode"
    53  	rsmGenerationLabelKey = "rsm.workloads.kubeblocks.io/controller-generation"
    54  
    55  	defaultPodName = "Unknown"
    56  
    57  	rsmFinalizerName = "rsm.workloads.kubeblocks.io/finalizer"
    58  
    59  	jobHandledLabel             = "rsm.workloads.kubeblocks.io/job-handled"
    60  	jobTypeLabel                = "rsm.workloads.kubeblocks.io/job-type"
    61  	jobScenarioLabel            = "rsm.workloads.kubeblocks.io/job-scenario"
    62  	jobHandledTrue              = "true"
    63  	jobHandledFalse             = "false"
    64  	jobTypeSwitchover           = "switchover"
    65  	jobTypeMemberJoinNotifying  = "member-join"
    66  	jobTypeMemberLeaveNotifying = "member-leave"
    67  	jobTypeLogSync              = "log-sync"
    68  	jobTypePromote              = "promote"
    69  	jobScenarioMembership       = "membership-reconfiguration"
    70  	jobScenarioUpdate           = "pod-update"
    71  
    72  	roleProbeContainerName       = "kb-role-probe"
    73  	roleProbeBinaryName          = "lorry"
    74  	roleAgentVolumeName          = "role-agent"
    75  	roleAgentInstallerName       = "role-agent-installer"
    76  	roleAgentVolumeMountPath     = "/role-probe"
    77  	roleAgentName                = "agent"
    78  	shell2httpImage              = "msoap/shell2http:1.16.0"
    79  	shell2httpBinaryPath         = "/app/shell2http"
    80  	shell2httpServePath          = "/role"
    81  	defaultRoleProbeAgentImage   = "apecloud/kubeblocks-tools:latest"
    82  	defaultRoleProbeDaemonPort   = 7373
    83  	defaultRoleProbeGRPCPort     = 50101
    84  	roleProbeGRPCPortName        = "probe-grpc-port"
    85  	httpRoleProbePath            = "/v1.0/checkrole"
    86  	grpcHealthProbeBinaryPath    = "/bin/grpc_health_probe"
    87  	grpcHealthProbeArgsFormat    = "-addr=:%d"
    88  	defaultActionImage           = "busybox:latest"
    89  	usernameCredentialVarName    = "KB_RSM_USERNAME"
    90  	passwordCredentialVarName    = "KB_RSM_PASSWORD"
    91  	servicePortVarName           = "KB_RSM_SERVICE_PORT"
    92  	actionSvcListVarName         = "KB_RSM_ACTION_SVC_LIST"
    93  	leaderHostVarName            = "KB_RSM_LEADER_HOST"
    94  	targetHostVarName            = "KB_RSM_TARGET_HOST"
    95  	RoleUpdateMechanismVarName   = "KB_RSM_ROLE_UPDATE_MECHANISM"
    96  	roleProbeTimeoutVarName      = "KB_RSM_ROLE_PROBE_TIMEOUT"
    97  	readinessProbeEventFieldPath = "spec.containers{" + roleProbeContainerName + "}"
    98  	legacyEventFieldPath         = "spec.containers{kb-checkrole}"
    99  	lorryEventFieldPath          = "spec.containers{kb-checkrole}"
   100  	checkRoleEventReason         = "checkRole"
   101  
   102  	actionSvcPortBase = int32(36500)
   103  )
   104  
   105  type rsmTransformContext struct {
   106  	context.Context
   107  	Client roclient.ReadonlyClient
   108  	record.EventRecorder
   109  	logr.Logger
   110  	rsm     *workloads.ReplicatedStateMachine
   111  	rsmOrig *workloads.ReplicatedStateMachine
   112  }
   113  
   114  func (c *rsmTransformContext) GetContext() context.Context {
   115  	return c.Context
   116  }
   117  
   118  func (c *rsmTransformContext) GetClient() roclient.ReadonlyClient {
   119  	return c.Client
   120  }
   121  
   122  func (c *rsmTransformContext) GetRecorder() record.EventRecorder {
   123  	return c.EventRecorder
   124  }
   125  
   126  func (c *rsmTransformContext) GetLogger() logr.Logger {
   127  	return c.Logger
   128  }
   129  
   130  var _ graph.TransformContext = &rsmTransformContext{}
   131  
   132  // AnnotationScope defines scope that annotations belong to.
   133  //
   134  // it is a common pattern to add annotations to extend the functionalities of K8s builtin resources.
   135  //
   136  // e.g.: Prometheus will start to scrape metrics if a service has annotation 'prometheus.io/scrape'.
   137  //
   138  // RSM has encapsulated K8s builtin resources like Service, Headless Service, StatefulSet, ConfigMap etc.
   139  // AnnotationScope specified a way to tell RSM controller which resource an annotation belongs to.
   140  //
   141  // e.g.:
   142  // let's say we want to add an annotation 'prometheus.io/scrape' with value 'true' to the underlying headless service.
   143  // here is what we should do:
   144  // add annotation 'prometheus.io/scrape' with an HeadlessServiceScope suffix to the RSM object's annotations field.
   145  //
   146  //	kind: ReplicatedStateMachine
   147  //	metadata:
   148  //	  annotations:
   149  //	    prometheus.io/scrape.headless.rsm: true
   150  //
   151  // the RSM controller will figure out which objects this annotation belongs to, cut the suffix and set it to the right place:
   152  //
   153  //	kind: Service
   154  //	metadata:
   155  //	  annotations:
   156  //	    prometheus.io/scrape: true
   157  type AnnotationScope string
   158  
   159  const (
   160  	// RootScope specifies the annotation belongs to the RSM object itself.
   161  	// they will also be set on the encapsulated StatefulSet.
   162  	RootScope AnnotationScope = ""
   163  
   164  	// HeadlessServiceScope specifies the annotation belongs to the encapsulated headless Service.
   165  	HeadlessServiceScope AnnotationScope = ".headless.rsm"
   166  
   167  	// ServiceScope specifies the annotation belongs to the encapsulated Service.
   168  	ServiceScope AnnotationScope = ".svc.rsm"
   169  
   170  	// AlternativeServiceScope specifies the annotation belongs to the encapsulated alternative Services.
   171  	AlternativeServiceScope AnnotationScope = ".alternative.rsm"
   172  
   173  	// ConfigMapScope specifies the annotation belongs to the encapsulated ConfigMap.
   174  	ConfigMapScope AnnotationScope = ".cm.rsm"
   175  )
   176  
   177  const scopeSuffix = ".rsm"