Skip to content

Commit

Permalink
Merge pull request #514 from yuleichun-striving/main
Browse files Browse the repository at this point in the history
fix: modify some codes about the bug of namespace is int
  • Loading branch information
kosmos-robot authored May 10, 2024
2 parents be27700 + c09e9b0 commit 87c2e25
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 37 deletions.
14 changes: 6 additions & 8 deletions examples/vitual-cluster-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ metadata:
namespace: test
name: test
spec:
externalIP: 127.0.0.10
promotePolicies:
- labelSelector:
matchLabels:
kubernetes.io/hostname: label1
nodeCount: 1
promoteResources:
nodeInfos:
- nodeName: nodeName1
address: 127.0.0.1
- nodeName: nodeName2
address: 127.0.0.2
resources:
cpu: 2
memory: "1Gi"
status:
phase: Completed
5 changes: 2 additions & 3 deletions pkg/kubenest/controlplane/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (

"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
Expand Down Expand Up @@ -71,7 +70,7 @@ func installAPIServer(client clientset.Interface, name, namespace string, port i
}

apiserverDeployment := &appsv1.Deployment{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), apiserverDeploymentBytes, apiserverDeployment); err != nil {
if err := yaml.Unmarshal([]byte(apiserverDeploymentBytes), apiserverDeployment); err != nil {
return fmt.Errorf("error when decoding virtual cluster apiserver deployment: %w", err)
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/kubenest/controlplane/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
Expand Down Expand Up @@ -133,7 +132,7 @@ func getKubeControllerManagerManifest(name, namespace string) (*appsv1.Deploymen
}

kcm := &appsv1.Deployment{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), kubeControllerManagerBytes, kcm); err != nil {
if err := yaml.Unmarshal([]byte(kubeControllerManagerBytes), kcm); err != nil {
return nil, fmt.Errorf("err when decoding kube-controller-manager deployment: %w", err)
}

Expand All @@ -152,7 +151,7 @@ func getVirtualClusterSchedulerConfigMapManifest(name, namespace string) (*v1.Co
}

config := &v1.ConfigMap{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), virtualClusterSchedulerConfigMapBytes, config); err != nil {
if err := yaml.Unmarshal([]byte(virtualClusterSchedulerConfigMapBytes), config); err != nil {
return nil, fmt.Errorf("err when decoding virtualCluster-scheduler configMap: %w", err)
}

Expand All @@ -179,7 +178,7 @@ func getVirtualClusterSchedulerManifest(name, namespace string) (*appsv1.Deploym
}

deploy := &appsv1.Deployment{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), virtualClusterSchedulerBytes, deploy); err != nil {
if err := yaml.Unmarshal([]byte(virtualClusterSchedulerBytes), deploy); err != nil {
return nil, fmt.Errorf("err when decoding virtualCluster-scheduler deployment: %w", err)
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/kubenest/controlplane/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (

"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/component-base/cli/flag"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
Expand Down Expand Up @@ -72,7 +71,7 @@ func installEtcd(client clientset.Interface, name, namespace string) error {
}

etcdStatefulSet := &appsv1.StatefulSet{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), etcdStatefulSetBytes, etcdStatefulSet); err != nil {
if err := yaml.Unmarshal([]byte(etcdStatefulSetBytes), etcdStatefulSet); err != nil {
return fmt.Errorf("error when decoding Etcd StatefulSet: %w", err)
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/kubenest/controlplane/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (

v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"

"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/scheduler"
"github.com/kosmos.io/kosmos/pkg/kubenest/util"
Expand Down Expand Up @@ -36,7 +35,7 @@ func grantVirtualClusterResourceClusterSA(client clientset.Interface, namespace
return fmt.Errorf("error when parsing virtualCluster-scheduler sa template: %w", err)
}
serviceAccount := &v1.ServiceAccount{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(virtualClusterResourceClusterSABytes), serviceAccount); err != nil {
if err := yaml.Unmarshal([]byte(virtualClusterResourceClusterSABytes), serviceAccount); err != nil {
return fmt.Errorf("err when decoding Karmada view Clusterrole: %w", err)
}
return util.CreateOrUpdateClusterSA(client, serviceAccount, namespace)
Expand All @@ -53,15 +52,15 @@ func grantVirtualClusterResourceClusterRoleBinding(client clientset.Interface, n
}
viewClusterRoleBinding := &rbacv1.ClusterRoleBinding{}

if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(virtualClusterResourceClusterRoleBindingBytes), viewClusterRoleBinding); err != nil {
if err := yaml.Unmarshal([]byte(virtualClusterResourceClusterRoleBindingBytes), viewClusterRoleBinding); err != nil {
return fmt.Errorf("err when decoding virtualCluster scheduler Clusterrole Binding: %w", err)
}
return util.CreateOrUpdateClusterRoleBinding(client, viewClusterRoleBinding)
}

func grantVirtualClusterResourceClusterRole(client clientset.Interface) error {
viewClusterrole := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(scheduler.VirtualSchedulerRole), viewClusterrole); err != nil {
if err := yaml.Unmarshal([]byte(scheduler.VirtualSchedulerRole), viewClusterrole); err != nil {
return fmt.Errorf("err when decoding virtualCluster scheduler Clusterrole: %w", err)
}
return util.CreateOrUpdateClusterRole(client, viewClusterrole)
Expand Down
9 changes: 4 additions & 5 deletions pkg/kubenest/controlplane/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
Expand Down Expand Up @@ -73,7 +72,7 @@ func createServerService(client clientset.Interface, name, namespace string, por
}

apiserverService := &corev1.Service{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), apiserverServiceBytes, apiserverService); err != nil {
if err := yaml.Unmarshal([]byte(apiserverServiceBytes), apiserverService); err != nil {
return fmt.Errorf("error when decoding virtual cluster apiserver service: %w", err)
}
if err := createOrUpdateService(client, apiserverService); err != nil {
Expand All @@ -94,7 +93,7 @@ func createServerService(client clientset.Interface, name, namespace string, por
}

etcdPeerService := &corev1.Service{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), etcdServicePeerBytes, etcdPeerService); err != nil {
if err := yaml.Unmarshal([]byte(etcdServicePeerBytes), etcdPeerService); err != nil {
return fmt.Errorf("error when decoding Etcd client service: %w", err)
}

Expand All @@ -115,7 +114,7 @@ func createServerService(client clientset.Interface, name, namespace string, por
}

etcdClientService := &corev1.Service{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), etcdClientServiceBytes, etcdClientService); err != nil {
if err := yaml.Unmarshal([]byte(etcdClientServiceBytes), etcdClientService); err != nil {
return fmt.Errorf("err when decoding Etcd client service: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/kubenest/tasks/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func applyYMLTemplate(dynamicClient dynamic.Interface, manifestGlob string, temp
return errors.Wrapf(err, "Parse template %s error", manifest)
}

err = yaml.Unmarshal(templateBytes, &obj)
err = yaml.Unmarshal([]byte(templateBytes), &obj)
if err != nil {
return errors.Wrapf(err, "Unmarshal manifest bytes data error")
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func deleteYMLTemplate(dynamicClient dynamic.Interface, manifestGlob string, tem
return errors.Wrapf(err, "Parse template %s error", manifest)
}

err = yaml.Unmarshal(templateBytes, &obj)
err = yaml.Unmarshal([]byte(templateBytes), &obj)
if err != nil {
return errors.Wrapf(err, "Unmarshal manifest bytes data error")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubenest/tasks/manifests_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ func applyTemplatedManifests(component string, dynamicClient dynamic.Interface,
templateData := bytesData
// template doesn't suit for prometheus rules, we deploy it directly
if component != constants.PrometheusRuleManifest {
templateData, err = util.ParseTemplate(string(bytesData), templateMapping)
templateString, err := util.ParseTemplate(string(bytesData), templateMapping)
if err != nil {
return errors.Wrapf(err, "Parse manifest file %s template error", manifest)
}
templateData = []byte(templateString)
}
err = yaml.Unmarshal(templateData, &obj)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/kubenest/util/cert/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func apiServerAltNamesMutator(cfg *AltNamesMutatorConfig) (*certutil.AltNames, e
//TODO (考虑节点属于当前集群节点和非当前集群节点情况)
IPs: []net.IP{
net.IPv4(127, 0, 0, 1),
net.IPv4(100, 71, 7, 52),
net.IPv4(10, 237, 6, 17),
net.IPv4(10, 237, 0, 1),
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/kubenest/util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

// ParseTemplate validates and parses passed as argument template
func ParseTemplate(strtmpl string, obj interface{}) ([]byte, error) {
func ParseTemplate(strtmpl string, obj interface{}) (string, error) {
var buf bytes.Buffer
tmpl, err := template.New("template").Parse(strtmpl)
if err != nil {
return nil, fmt.Errorf("error when parsing template, err: %w", err)
return "", fmt.Errorf("error when parsing template, err: %w", err)
}
err = tmpl.Execute(&buf, obj)
if err != nil {
return nil, fmt.Errorf("error when executing template, err: %w", err)
return "", fmt.Errorf("error when executing template, err: %w", err)
}
return buf.Bytes(), nil
return buf.String(), nil
}

0 comments on commit 87c2e25

Please sign in to comment.