-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/component-definition' of github.com:apecloud/ku…
…beblocks into feature/component-definition
- Loading branch information
Showing
10 changed files
with
405 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright (C) 2022-2023 ApeCloud Co., Ltd | ||
This file is part of KubeBlocks project | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package apps | ||
|
||
import ( | ||
"fmt" | ||
|
||
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" | ||
"github.com/apecloud/kubeblocks/pkg/controller/apiconversion" | ||
"github.com/apecloud/kubeblocks/pkg/controller/component" | ||
"github.com/apecloud/kubeblocks/pkg/controller/graph" | ||
"github.com/apecloud/kubeblocks/pkg/controller/model" | ||
) | ||
|
||
// ClusterAPINormalizationTransformer handles cluster and component API conversion. | ||
type ClusterAPINormalizationTransformer struct{} | ||
|
||
var _ graph.Transformer = &ClusterAPINormalizationTransformer{} | ||
|
||
func (t *ClusterAPINormalizationTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error { | ||
transCtx, _ := ctx.(*clusterTransformContext) | ||
if model.IsObjectDeleting(transCtx.OrigCluster) { | ||
return nil | ||
} | ||
|
||
// build all component specs | ||
transCtx.ComponentSpecs = make([]*appsv1alpha1.ClusterComponentSpec, 0) | ||
cluster := transCtx.Cluster | ||
for i := range cluster.Spec.ComponentSpecs { | ||
transCtx.ComponentSpecs = append(transCtx.ComponentSpecs, &cluster.Spec.ComponentSpecs[i]) | ||
} | ||
if compSpec := apiconversion.HandleSimplifiedClusterAPI(cluster, transCtx.ClusterDef); compSpec != nil { | ||
transCtx.ComponentSpecs = append(transCtx.ComponentSpecs, compSpec) | ||
} | ||
|
||
// build all component definitions referenced | ||
if transCtx.ComponentDefs == nil { | ||
transCtx.ComponentDefs = make(map[string]*appsv1alpha1.ComponentDefinition) | ||
} | ||
for i, compSpec := range transCtx.ComponentSpecs { | ||
if len(compSpec.ComponentDef) == 0 { | ||
compDef, err := t.buildComponentDefinition(transCtx, compSpec) | ||
if err != nil { | ||
return err | ||
} | ||
transCtx.ComponentDefs[compDef.Name] = compDef | ||
transCtx.ComponentSpecs[i].ComponentDef = compDef.Name | ||
} else { | ||
if _, ok := transCtx.ComponentDefs[compSpec.ComponentDef]; !ok { | ||
panic(fmt.Sprintf("runtime error - expected component definition object not found: %s", compSpec.ComponentDef)) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (t *ClusterAPINormalizationTransformer) buildComponentDefinition(transCtx *clusterTransformContext, | ||
clusterCompSpec *appsv1alpha1.ClusterComponentSpec) (*appsv1alpha1.ComponentDefinition, error) { | ||
if clusterCompSpec.ComponentDefRef == "" { | ||
return nil, fmt.Errorf("expected cluster component def ref is empty: %s-%s", transCtx.Cluster.Name, clusterCompSpec.Name) | ||
} | ||
clusterCompDef := transCtx.ClusterDef.GetComponentDefByName(clusterCompSpec.ComponentDefRef) | ||
if clusterCompDef == nil { | ||
return nil, fmt.Errorf("referenced cluster component def is not defined: %s-%s", transCtx.Cluster.Name, clusterCompSpec.Name) | ||
} | ||
var clusterCompVer *appsv1alpha1.ClusterComponentVersion | ||
if transCtx.ClusterVer != nil { | ||
clusterCompVer = transCtx.ClusterVer.Spec.GetDefNameMappingComponents()[clusterCompSpec.ComponentDefRef] | ||
} | ||
return component.BuildComponentDefinitionFrom(clusterCompDef, clusterCompVer, transCtx.Cluster.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.