github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/rsm/transformer_status.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  	"strconv"
    24  
    25  	apps "k8s.io/api/apps/v1"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/1aal/kubeblocks/pkg/controller/graph"
    29  	"github.com/1aal/kubeblocks/pkg/controller/model"
    30  )
    31  
    32  // ObjectStatusTransformer computes the current status:
    33  // 1. read the underlying sts's status and copy them to the primary object's status
    34  // 2. read pod role label and update the primary object's status role fields
    35  type ObjectStatusTransformer struct{}
    36  
    37  var _ graph.Transformer = &ObjectStatusTransformer{}
    38  
    39  func (t *ObjectStatusTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
    40  	transCtx, _ := ctx.(*rsmTransformContext)
    41  	rsm := transCtx.rsm
    42  	rsmOrig := transCtx.rsmOrig
    43  
    44  	// fast return
    45  	if model.IsObjectDeleting(rsmOrig) {
    46  		return nil
    47  	}
    48  
    49  	switch {
    50  	case model.IsObjectUpdating(rsmOrig):
    51  		// use rsm's generation instead of sts's
    52  		rsm.Status.ObservedGeneration = rsm.Generation
    53  	case model.IsObjectStatusUpdating(rsmOrig):
    54  		// read the underlying sts
    55  		sts := &apps.StatefulSet{}
    56  		if err := transCtx.Client.Get(transCtx.Context, client.ObjectKeyFromObject(rsm), sts); err != nil {
    57  			return err
    58  		}
    59  		// keep rsm's ObservedGeneration to avoid override by sts's ObservedGeneration
    60  		generation := rsm.Status.ObservedGeneration
    61  		rsm.Status.StatefulSetStatus = sts.Status
    62  		rsm.Status.ObservedGeneration = generation
    63  		if currentGenerationLabel, ok := sts.Labels[rsmGenerationLabelKey]; ok {
    64  			currentGeneration, err := strconv.ParseInt(currentGenerationLabel, 10, 64)
    65  			if err != nil {
    66  				return err
    67  			}
    68  			rsm.Status.CurrentGeneration = currentGeneration
    69  		}
    70  		// read all pods belong to the sts, hence belong to the rsm
    71  		pods, err := getPodsOfStatefulSet(transCtx.Context, transCtx.Client, sts)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		// update role fields
    76  		setMembersStatus(rsm, pods)
    77  	}
    78  
    79  	graphCli, _ := transCtx.Client.(model.GraphClient)
    80  	graphCli.Status(dag, rsmOrig, rsm)
    81  
    82  	return nil
    83  }