github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/components/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 components
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	"github.com/1aal/kubeblocks/pkg/class"
    30  	"github.com/1aal/kubeblocks/pkg/constant"
    31  	types2 "github.com/1aal/kubeblocks/pkg/controller/client"
    32  	"github.com/1aal/kubeblocks/pkg/controller/component"
    33  	"github.com/1aal/kubeblocks/pkg/controller/graph"
    34  	"github.com/1aal/kubeblocks/pkg/controller/plan"
    35  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    36  )
    37  
    38  type Component interface {
    39  	GetName() string
    40  	GetNamespace() string
    41  	GetClusterName() string
    42  
    43  	GetCluster() *appsv1alpha1.Cluster
    44  	GetClusterVersion() *appsv1alpha1.ClusterVersion
    45  	GetSynthesizedComponent() *component.SynthesizedComponent
    46  
    47  	Create(reqCtx intctrlutil.RequestCtx, cli client.Client) error
    48  	Delete(reqCtx intctrlutil.RequestCtx, cli client.Client) error
    49  	Update(reqCtx intctrlutil.RequestCtx, cli client.Client) error
    50  	Status(reqCtx intctrlutil.RequestCtx, cli client.Client) error
    51  }
    52  
    53  func NewComponent(reqCtx intctrlutil.RequestCtx,
    54  	cli client.Client,
    55  	definition *appsv1alpha1.ClusterDefinition,
    56  	version *appsv1alpha1.ClusterVersion,
    57  	cluster *appsv1alpha1.Cluster,
    58  	compName string,
    59  	dag *graph.DAG) (Component, error) {
    60  	var compDef *appsv1alpha1.ClusterComponentDefinition
    61  	var compVer *appsv1alpha1.ClusterComponentVersion
    62  	compSpec := cluster.Spec.GetComponentByName(compName)
    63  	if compSpec != nil {
    64  		compDef = definition.GetComponentDefByName(compSpec.ComponentDefRef)
    65  		if compDef == nil {
    66  			return nil, fmt.Errorf("referenced component definition does not exist, cluster: %s, component: %s, component definition ref:%s",
    67  				cluster.Name, compSpec.Name, compSpec.ComponentDefRef)
    68  		}
    69  		if version != nil {
    70  			compVer = version.Spec.GetDefNameMappingComponents()[compSpec.ComponentDefRef]
    71  		}
    72  	} else {
    73  		compDef = definition.GetComponentDefByName(compName)
    74  		if version != nil {
    75  			compVer = version.Spec.GetDefNameMappingComponents()[compName]
    76  		}
    77  	}
    78  
    79  	if compDef == nil {
    80  		return nil, nil
    81  	}
    82  
    83  	clsMgr, err := getClassManager(reqCtx.Ctx, cli, cluster)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	serviceReferences, err := plan.GenServiceReferences(reqCtx, cli, cluster, compDef, compSpec)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	synthesizedComp, err := component.BuildComponent(reqCtx, clsMgr, cluster, definition, compDef, compSpec, serviceReferences, compVer)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	if synthesizedComp == nil {
    97  		return nil, nil
    98  	}
    99  
   100  	return newRSMComponent(cli, reqCtx.Recorder, cluster, version, synthesizedComp, dag), nil
   101  }
   102  
   103  func getClassManager(ctx context.Context, cli types2.ReadonlyClient, cluster *appsv1alpha1.Cluster) (*class.Manager, error) {
   104  	var classDefinitionList appsv1alpha1.ComponentClassDefinitionList
   105  	ml := []client.ListOption{
   106  		client.MatchingLabels{constant.ClusterDefLabelKey: cluster.Spec.ClusterDefRef},
   107  	}
   108  	if err := cli.List(ctx, &classDefinitionList, ml...); err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	var constraintList appsv1alpha1.ComponentResourceConstraintList
   113  	if err := cli.List(ctx, &constraintList); err != nil {
   114  		return nil, err
   115  	}
   116  	return class.NewManager(classDefinitionList, constraintList)
   117  }