Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yurtctl devfor ut #947

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions pkg/yurtctl/cmd/yurttest/kindinit/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
kubeclientset "k8s.io/client-go/kubernetes"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
"k8s.io/klog/v2"

Expand All @@ -45,7 +45,7 @@ const (
)

type ClusterConverter struct {
ClientSet *kubernetes.Clientset
ClientSet kubeclientset.Interface
CloudNodes []string
EdgeNodes []string
WaitServantJobTimeout time.Duration
Expand Down Expand Up @@ -179,7 +179,7 @@ func (c *ClusterConverter) deployYurthub() error {
return nil
}

func prepareYurthubStart(cliSet *kubernetes.Clientset, kcfg string) (string, error) {
func prepareYurthubStart(cliSet kubeclientset.Interface, kcfg string) (string, error) {
// prepare kube-public/cluster-info configmap before convert
if err := prepareClusterInfoConfigMap(cliSet, kcfg); err != nil {
return "", err
Expand All @@ -199,7 +199,7 @@ func prepareYurthubStart(cliSet *kubernetes.Clientset, kcfg string) (string, err
}

// prepareClusterInfoConfigMap will create cluster-info configmap in kube-public namespace if it does not exist
func prepareClusterInfoConfigMap(client *kubernetes.Clientset, file string) error {
func prepareClusterInfoConfigMap(client kubeclientset.Interface, file string) error {
info, err := client.CoreV1().ConfigMaps(v1.NamespacePublic).Get(context.Background(), bootstrapapi.ConfigMapClusterInfo, v1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
// Create the cluster-info ConfigMap with the associated RBAC rules
Expand All @@ -214,11 +214,10 @@ func prepareClusterInfoConfigMap(client *kubernetes.Clientset, file string) erro
} else {
klog.V(4).Infof("%s/%s configmap already exists, skip to prepare it", info.Namespace, info.Name)
}

return nil
}

func nodePoolResourceExists(client *kubernetes.Clientset) (bool, error) {
func nodePoolResourceExists(client kubeclientset.Interface) (bool, error) {
groupVersion := schema.GroupVersion{
Group: "apps.openyurt.io",
Version: "v1alpha1",
Expand All @@ -236,6 +235,5 @@ func nodePoolResourceExists(client *kubernetes.Clientset) (bool, error) {
return true, nil
}
}

return false, nil
}
218 changes: 218 additions & 0 deletions pkg/yurtctl/cmd/yurttest/kindinit/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
Copyright 2022 The OpenYurt Authors.

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

http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kindinit

import (
"io"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientsetfake "k8s.io/client-go/kubernetes/fake"

yurtutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/kubernetes"
)

func NewClusterConverter(ki *Initializer) *ClusterConverter {
converter := &ClusterConverter{
ClientSet: ki.kubeClient,
CloudNodes: ki.CloudNodes,
EdgeNodes: ki.EdgeNodes,
WaitServantJobTimeout: yurtutil.DefaultWaitServantJobTimeout,
YurthubHealthCheckTimeout: defaultYurthubHealthCheckTimeout,
KubeConfigPath: ki.KubeConfig,
YurtTunnelAgentImage: ki.YurtTunnelAgentImage,
YurtTunnelServerImage: ki.YurtTunnelServerImage,
YurtControllerManagerImage: ki.YurtControllerManagerImage,
NodeServantImage: ki.NodeServantImage,
YurthubImage: ki.YurtHubImage,
EnableDummyIf: ki.EnableDummyIf,
}
return converter
}

func TestClusterConverter_LabelEdgeNodes(t *testing.T) {
case1 := struct {
podObj *corev1.Node
want error
}{
podObj: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "openyurt-worker",
Namespace: "",
Labels: map[string]string{},
Annotations: map[string]string{},
},
},
want: nil,
}
var fakeOut io.Writer
initializer := newKindInitializer(fakeOut, newKindOptions().Config())
initializer.kubeClient = clientsetfake.NewSimpleClientset(case1.podObj)
converter := NewClusterConverter(initializer)
if converter.labelEdgeNodes() != case1.want {
t.Errorf("failed to label edge nodes")
}
}

func TestClusterConverter_DeployYurtTunnel(t *testing.T) {

cases := []struct {
YurtTunnelServerImage string
YurtTunnelAgentImage string
want error
}{
{
YurtTunnelServerImage: "openyurt/yurt-tunnel-server:v0.6.0",
YurtTunnelAgentImage: "openyurt/yurt-tunnel-agent:v0.6.0",
want: nil,
},
{

YurtTunnelServerImage: "openyurt/yurt-tunnel-server:v0.12.0",
YurtTunnelAgentImage: "openyurt/yurt-tunnel-agent:v0.12.0",
want: nil,
},
}

var fakeOut io.Writer
initializer := newKindInitializer(fakeOut, newKindOptions().Config())
initializer.kubeClient = clientsetfake.NewSimpleClientset()
converter := NewClusterConverter(initializer)
for _, v := range cases {
converter.YurtTunnelServerImage = v.YurtTunnelServerImage
converter.YurtTunnelAgentImage = v.YurtTunnelAgentImage
err := converter.deployYurtTunnel()
if err != v.want {
t.Errorf("failed to deploy yurt tunnel")
}
}
}

func TestPrepareClusterInfoConfigMap(t *testing.T) {
case1 := struct {
configObj *corev1.ConfigMap
want error
}{
configObj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Namespace: "kube-public", Name: "cluster-info"},
},
want: nil,
}
fakeKubeClient := clientsetfake.NewSimpleClientset(case1.configObj)
err := prepareClusterInfoConfigMap(fakeKubeClient, "")
if err != case1.want {
t.Errorf("failed to prepare the cluster information of ConfigMap ")
}
}

func TestPrepareYurthubStart(t *testing.T) {
case1 := struct {
configObj *corev1.ConfigMap
want error
}{
configObj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Namespace: "kube-public", Name: "cluster-info"},
},
want: nil,
}
fakeKubeClient := clientsetfake.NewSimpleClientset(case1.configObj)
joinToken, err := prepareYurthubStart(fakeKubeClient, "")
if err != case1.want && joinToken == "" {
t.Errorf("failed to prepare yurthub start ")
}
}

func TestNodePoolResourceExists(t *testing.T) {

cases := []struct {
apiResourceObj *metav1.APIResourceList
want error
isExist bool
}{
{
apiResourceObj: &metav1.APIResourceList{
GroupVersion: "apps.openyurt.io/v1alpha1",
APIResources: []metav1.APIResource{
{
Name: "nodepools",
Kind: "NodePool",
},
},
},
want: nil,
isExist: true,
},
{
apiResourceObj: &metav1.APIResourceList{
GroupVersion: "apps.openyurt.io/v1alpha1",
APIResources: []metav1.APIResource{
{
Name: "nodepools",
Kind: "pod",
},
},
},
want: nil,
isExist: false,
},
}
for _, v := range cases {
fakeKubeClient := clientsetfake.NewSimpleClientset()
fakeKubeClient.Fake.Resources = append(fakeKubeClient.Fake.Resources, v.apiResourceObj)
isExist, err := nodePoolResourceExists(fakeKubeClient)
if err != v.want && isExist != v.isExist {
t.Errorf("falied to verify nodes pool is exist")
}
}
}

func TestClusterConverter_DeployYurtHub(t *testing.T) {
case1 := struct {
configObj *corev1.ConfigMap
apiResourceObj *metav1.APIResourceList
want error
}{
configObj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Namespace: "kube-public", Name: "cluster-info"},
},
apiResourceObj: &metav1.APIResourceList{
GroupVersion: "apps.openyurt.io/v1alpha1",
APIResources: []metav1.APIResource{
{
Name: "nodepools",
Kind: "NodePool",
},
},
},
want: nil,
}

var fakeOut io.Writer
initializer := newKindInitializer(fakeOut, newKindOptions().Config())
fakeclient := clientsetfake.NewSimpleClientset(case1.configObj)
fakeclient.Fake.Resources = append(fakeclient.Fake.Resources, case1.apiResourceObj)
initializer.kubeClient = fakeclient
converter := NewClusterConverter(initializer)
converter.CloudNodes = []string{}
converter.EdgeNodes = []string{}
err := converter.deployYurthub()
if err != case1.want {
t.Errorf("failed to deploy yurthub")
}

}
7 changes: 4 additions & 3 deletions pkg/yurtctl/cmd/yurttest/kindinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
kubeclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -261,7 +261,7 @@ type Initializer struct {
initializerConfig
out io.Writer
operator *KindOperator
kubeClient *kubernetes.Clientset
kubeClient kubeclientset.Interface
}

func newKindInitializer(out io.Writer, cfg *initializerConfig) *Initializer {
Expand Down Expand Up @@ -298,7 +298,7 @@ func (ki *Initializer) Run() error {
if err != nil {
return err
}
ki.kubeClient, err = kubernetes.NewForConfig(kubeconfig)
ki.kubeClient, err = kubeclientset.NewForConfig(kubeconfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -538,6 +538,7 @@ func (ki *Initializer) configureCoreDnsAddon() error {
break
}
}

if !hasEdgeVolumeMount {
dp.Spec.Template.Spec.Containers[containerIndex].VolumeMounts = append(dp.Spec.Template.Spec.Containers[containerIndex].VolumeMounts,
v1.VolumeMount{
Expand Down
Loading