Skip to content

Commit

Permalink
feat(platform): support deploy custom k8s (#1029)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Ryu committed Dec 29, 2020
1 parent 72162de commit 1d75003
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/platform/provider/baremetal/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (p *Provider) RegisterHandler(mux *mux.PathRecorderMux) {
}

func (p *Provider) Validate(cluster *types.Cluster) field.ErrorList {
return validation.ValidateCluster(cluster)
return validation.ValidateCluster(p.platformClient, cluster)
}

func (p *Provider) PreCreate(cluster *types.Cluster) error {
Expand Down
36 changes: 30 additions & 6 deletions pkg/platform/provider/baremetal/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package validation

import (
"context"
"fmt"
"net"
"strings"
Expand All @@ -27,9 +28,11 @@ import (
"tkestack.io/tke/api/platform"

netutils "k8s.io/utils/net"
platformv1client "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
csioperatorimage "tkestack.io/tke/pkg/platform/provider/baremetal/phases/csioperator/images"
"tkestack.io/tke/pkg/platform/provider/baremetal/phases/gpu"
"tkestack.io/tke/pkg/platform/types"
"tkestack.io/tke/pkg/platform/util"
"tkestack.io/tke/pkg/spec"
"tkestack.io/tke/pkg/util/ipallocator"
"tkestack.io/tke/pkg/util/validation"
Expand All @@ -42,17 +45,17 @@ var (
)

// ValidateCluster validates a given Cluster.
func ValidateCluster(obj *types.Cluster) field.ErrorList {
allErrs := ValidatClusterSpec(&obj.Spec, field.NewPath("spec"), obj.Status.Phase)
func ValidateCluster(platformClient platformv1client.PlatformV1Interface, obj *types.Cluster) field.ErrorList {
allErrs := ValidatClusterSpec(platformClient, obj.Name, &obj.Spec, field.NewPath("spec"), obj.Status.Phase)

return allErrs
}

// ValidatClusterSpec validates a given ClusterSpec.
func ValidatClusterSpec(spec *platform.ClusterSpec, fldPath *field.Path, phase platform.ClusterPhase) field.ErrorList {
func ValidatClusterSpec(platformClient platformv1client.PlatformV1Interface, clusterName string, spec *platform.ClusterSpec, fldPath *field.Path, phase platform.ClusterPhase) field.ErrorList {
allErrs := field.ErrorList{}

allErrs = append(allErrs, ValidateClusterSpecVersion(spec.Version, fldPath.Child("version"), phase)...)
allErrs = append(allErrs, ValidateClusterSpecVersion(platformClient, clusterName, spec.Version, fldPath.Child("version"), phase)...)
allErrs = append(allErrs, ValidateCIDRs(spec, fldPath)...)
allErrs = append(allErrs, ValidateClusterProperty(spec, fldPath.Child("properties"))...)
allErrs = append(allErrs, ValidateClusterMachines(spec.Machines, fldPath.Child("machines"))...)
Expand All @@ -62,15 +65,36 @@ func ValidatClusterSpec(spec *platform.ClusterSpec, fldPath *field.Path, phase p
}

// ValidateClusterSpecVersion validates a given version.
func ValidateClusterSpecVersion(version string, fldPath *field.Path, phase platform.ClusterPhase) field.ErrorList {
func ValidateClusterSpecVersion(platformClient platformv1client.PlatformV1Interface, clsName, version string, fldPath *field.Path, phase platform.ClusterPhase) field.ErrorList {
allErrs := field.ErrorList{}

k8sValidVersions, err := getK8sValidVersions(platformClient, clsName)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath, err))
return allErrs
}

if phase == platform.ClusterInitializing {
allErrs = utilvalidation.ValidateEnum(version, fldPath, spec.K8sVersions)
allErrs = utilvalidation.ValidateEnum(version, fldPath, k8sValidVersions)
}

return allErrs
}

func getK8sValidVersions(platformClient platformv1client.PlatformV1Interface, clsName string) (validVersions []string, err error) {
k8sValidVersions := []string{}
if clsName == "global" || platformClient == nil {
return spec.K8sVersions, nil
}
client, err := util.BuildExternalClientSetWithName(context.Background(), platformClient, "global")
if err != nil {
return k8sValidVersions, err
}
_, k8sValidVersions, err = util.GetPlatformVersionsFromClusterInfo(context.Background(), client)

return k8sValidVersions, err
}

// ValidateCIDRs validates clusterCIDR and serviceCIDR.
func ValidateCIDRs(spec *platform.ClusterSpec, specPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
Expand Down
41 changes: 41 additions & 0 deletions pkg/platform/util/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2020 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package util

import (
"context"
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func GetPlatformVersionsFromClusterInfo(ctx context.Context, client kubernetes.Interface) (tkeVersion string, k8sValidVersions []string, err error) {
k8sValidVersions = []string{}
clusterInfo, err := client.CoreV1().ConfigMaps("kube-public").Get(ctx, "cluster-info", metav1.GetOptions{})
if err != nil {
return
}
tkeVersion, ok := clusterInfo.Data["tkeVersion"]
if !ok {
return
}
err = json.Unmarshal([]byte(clusterInfo.Data["k8sValidVersions"]), &k8sValidVersions)
return
}

0 comments on commit 1d75003

Please sign in to comment.