Skip to content

Commit

Permalink
chore: filtering v1 version events fails when no changes have been ma…
Browse files Browse the repository at this point in the history
…de to the ClusterDefinition. (#8612)
  • Loading branch information
wangyelei authored Dec 11, 2024
1 parent 248487d commit 3d89b29
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 deletions.
3 changes: 3 additions & 0 deletions apis/apps/v1alpha1/cluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type clusterConverter struct {
}

type clusterSpecConverter struct {
ClusterDefRef string `json:"clusterDefRef,omitempty"`
ClusterVersionRef string `json:"clusterVersionRef,omitempty"`
TerminationPolicy TerminationPolicyType `json:"terminationPolicy"`
Affinity *Affinity `json:"affinity,omitempty"`
Expand Down Expand Up @@ -193,6 +194,7 @@ type clusterCompStatusConverter struct {

func (c *clusterConverter) fromCluster(cluster *Cluster) {
c.Spec.ClusterVersionRef = cluster.Spec.ClusterVersionRef
c.Spec.ClusterDefRef = cluster.Spec.ClusterDefRef
c.Spec.TerminationPolicy = cluster.Spec.TerminationPolicy
c.Spec.Affinity = cluster.Spec.Affinity
c.Spec.Tolerations = cluster.Spec.Tolerations
Expand Down Expand Up @@ -244,6 +246,7 @@ func (c *clusterConverter) fromCluster(cluster *Cluster) {

func (c *clusterConverter) toCluster(cluster *Cluster) {
cluster.Spec.ClusterVersionRef = c.Spec.ClusterVersionRef
cluster.Spec.ClusterDefRef = c.Spec.ClusterDefRef
cluster.Spec.TerminationPolicy = c.Spec.TerminationPolicy
cluster.Spec.Affinity = c.Spec.Affinity
cluster.Spec.Tolerations = c.Spec.Tolerations
Expand Down
16 changes: 15 additions & 1 deletion apis/apps/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -77,13 +78,26 @@ func incrementConvertFrom(converter incrementConverter, source metav1.Object, ic
if annotations != nil {
data, ok := annotations[kbIncrementConverterAK]
if ok {
// Convert from the incremental converter only if the annotation exists.
if err := json.Unmarshal([]byte(data), ic); err != nil {
return err
}
delete(annotations, kbIncrementConverterAK)
source.SetAnnotations(annotations)
return converter.incrementConvertFrom(source, ic)
}
}
return nil
}

return converter.incrementConvertFrom(source, ic)
func GetClusterDefFromIncrementConverter(obj client.Object) (string, error) {
incrementConverterStr := obj.GetAnnotations()[kbIncrementConverterAK]
if len(incrementConverterStr) == 0 {
return "", nil
}
var alpha1Cluster Cluster
if err := json.Unmarshal([]byte(incrementConverterStr), &alpha1Cluster); err != nil {
return "", err
}
return alpha1Cluster.Spec.ClusterDefRef, nil
}
65 changes: 42 additions & 23 deletions controllers/apps/transformer_cluster_normalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/graph"
Expand Down Expand Up @@ -416,37 +417,55 @@ func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx *
}

func (t *clusterNormalizationTransformer) checkNPatchCRDAPIVersionKey(transCtx *clusterTransformContext) error {
apiVersions := map[string][]string{}
getCRDAPIVersion := func() (string, error) {
apiVersion := transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey]
if len(apiVersion) > 0 {
return apiVersion, nil
}
// check if the cluster is the alpha1 version
clusterDefRef, err := appsv1alpha1.GetClusterDefFromIncrementConverter(transCtx.Cluster)
if err != nil {
return "", err
}
if len(clusterDefRef) > 0 {
return appsv1alpha1.GroupVersion.String(), nil
}

from := func(name string, annotations map[string]string) {
key := annotations[constant.CRDAPIVersionAnnotationKey]
apiVersions[key] = append(apiVersions[key], name)
}
// get the CRD API version from the annotations of the clusterDef or componentDefs
apiVersions := map[string][]string{}
from := func(name string, annotations map[string]string) {
key := annotations[constant.CRDAPIVersionAnnotationKey]
apiVersions[key] = append(apiVersions[key], name)
}

if transCtx.clusterDef != nil {
from(transCtx.clusterDef.Name, transCtx.clusterDef.Annotations)
} else {
for _, compDef := range transCtx.componentDefs {
from(compDef.Name, compDef.Annotations)
if transCtx.clusterDef != nil {
from(transCtx.clusterDef.Name, transCtx.clusterDef.Annotations)
} else {
for _, compDef := range transCtx.componentDefs {
from(compDef.Name, compDef.Annotations)
}
for _, shardingDef := range transCtx.shardingDefs {
from(shardingDef.Name, shardingDef.Annotations)
}
}
for _, shardingDef := range transCtx.shardingDefs {
from(shardingDef.Name, shardingDef.Annotations)
switch {
case len(apiVersions) > 1:
return "", fmt.Errorf("multiple CRD API versions found: %v", apiVersions)
case len(apiVersions) == 1:
return maps.Keys(apiVersions)[0], nil
default:
return "", nil
}
}

if len(apiVersions) > 1 {
return fmt.Errorf("multiple CRD API versions found: %v", apiVersions)
apiVersion, err := getCRDAPIVersion()
if err != nil {
return err
}

apiVersion := ""
if len(apiVersions) == 1 {
apiVersion = maps.Keys(apiVersions)[0]
if transCtx.Cluster.Annotations == nil {
transCtx.Cluster.Annotations = make(map[string]string)
}
transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] = apiVersion
if transCtx.Cluster.Annotations == nil {
transCtx.Cluster.Annotations = make(map[string]string)
}

transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] = apiVersion
if controllerutil.IsAPIVersionSupported(apiVersion) {
return nil
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/apps/transformer_component_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ var _ graph.Transformer = &componentInitTransformer{}
func (t *componentInitTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
transCtx, _ := ctx.(*componentTransformContext)

if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) {
return graph.ErrPrematureStop
}

// init dag
rootVertex := &model.ObjectVertex{Obj: transCtx.Component, OriObj: transCtx.ComponentOrig, Action: model.ActionStatusPtr()}
dag.AddVertex(rootVertex)

// init placement
transCtx.Context = intoContext(transCtx.Context, placement(transCtx.Component))

if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) {
return graph.ErrPrematureStop
}
return nil
}
26 changes: 26 additions & 0 deletions deploy/helm/templates/rbac/apps_sidecardefinition_editor_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# permissions for end users to edit sidecardefinitions.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "kubeblocks.fullname" . }}-sidecardefinition-editor-role
labels:
{{- include "kubeblocks.labels" . | nindent 4 }}
rules:
- apiGroups:
- apps.kubeblocks.io
resources:
- sidecardefinitions
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps.kubeblocks.io
resources:
- sidecardefinitions/status
verbs:
- get

0 comments on commit 3d89b29

Please sign in to comment.