-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: huiwq1990 <[email protected]>
- Loading branch information
Showing
6 changed files
with
293 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
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 util | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/openyurtio/openyurt/pkg/apis/apps" | ||
"github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" | ||
) | ||
|
||
func LabelNodeIsEdge(ctx context.Context, k8sClient client.Client) error { | ||
nodes := &corev1.NodeList{} | ||
if err := k8sClient.List(ctx, nodes); err != nil { | ||
return err | ||
} | ||
|
||
for _, originNode := range nodes.Items { | ||
nodeLabels := originNode.Labels | ||
if nodeLabels == nil { | ||
nodeLabels = map[string]string{} | ||
} | ||
switch originNode.Name { | ||
case "openyurt-e2e-test-control-plane": | ||
nodeLabels["openyurt.io/is-edge-worker"] = "false" | ||
default: | ||
nodeLabels["openyurt.io/is-edge-worker"] = "true" | ||
} | ||
|
||
newNode := originNode.DeepCopy() | ||
newNode.Labels = nodeLabels | ||
if err := k8sClient.Patch(ctx, newNode, client.MergeFrom(&originNode)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func CleanupNodePool(ctx context.Context, k8sClient client.Client) error { | ||
nps := &v1beta1.NodePoolList{} | ||
if err := k8sClient.List(ctx, nps); err != nil { | ||
return err | ||
} | ||
for _, tmp := range nps.Items { | ||
if err := k8sClient.Delete(ctx, &tmp); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func CleanupNodeLabel(ctx context.Context, k8sClient client.Client) error { | ||
nodes := &corev1.NodeList{} | ||
if err := k8sClient.List(ctx, nodes); err != nil { | ||
return err | ||
} | ||
|
||
for _, originNode := range nodes.Items { | ||
newNode := originNode.DeepCopy() | ||
if newNode.Labels != nil { | ||
for k := range newNode.Labels { | ||
if k == apps.LabelDesiredNodePool { | ||
delete(newNode.Labels, apps.LabelDesiredNodePool) | ||
} | ||
if k == "openyurt.io/is-edge-worker" { | ||
delete(newNode.Labels, "openyurt.io/is-edge-worker") | ||
} | ||
} | ||
} | ||
|
||
if err := k8sClient.Patch(context.TODO(), newNode, client.MergeFrom(&originNode)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func InitNodeAndNodePool(ctx context.Context, k8sClient client.Client, poolToNodesMap map[string]sets.String) error { | ||
nodeToPoolMap := make(map[string]string) | ||
for k, v := range poolToNodesMap { | ||
for _, n := range v.List() { | ||
nodeToPoolMap[n] = k | ||
} | ||
} | ||
|
||
for k := range poolToNodesMap { | ||
if err := k8sClient.Create(ctx, &v1beta1.NodePool{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: k, | ||
}, | ||
Spec: v1beta1.NodePoolSpec{ | ||
Type: v1beta1.Edge, | ||
}}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
nodes := &corev1.NodeList{} | ||
if err := k8sClient.List(ctx, nodes); err != nil { | ||
return err | ||
} | ||
|
||
for _, originNode := range nodes.Items { | ||
newNode := originNode.DeepCopy() | ||
nodeLabels := newNode.Labels | ||
if nodeLabels == nil { | ||
nodeLabels = map[string]string{} | ||
} | ||
|
||
if _, ok := nodeToPoolMap[originNode.Name]; !ok { | ||
continue | ||
} | ||
|
||
nodeLabels[apps.LabelDesiredNodePool] = nodeToPoolMap[originNode.Name] | ||
nodeLabels["openyurt.io/is-edge-worker"] = "true" | ||
newNode.Labels = nodeLabels | ||
if err := k8sClient.Patch(ctx, newNode, client.MergeFrom(&originNode)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,117 @@ | ||
/* | ||
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 yurt | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" | ||
"github.com/openyurtio/openyurt/test/e2e/util" | ||
ycfg "github.com/openyurtio/openyurt/test/e2e/yurtconfig" | ||
) | ||
|
||
var _ = Describe("nodepool test", func() { | ||
ctx := context.Background() | ||
var k8sClient runtimeclient.Client | ||
poolToNodesMap := make(map[string]sets.String) | ||
|
||
checkNodePoolStatus := func(poolToNodesMap map[string]sets.String) error { | ||
nps := &v1beta1.NodePoolList{} | ||
if err := k8sClient.List(ctx, nps); err != nil { | ||
return err | ||
} | ||
for _, tmp := range nps.Items { | ||
if int(tmp.Status.ReadyNodeNum) != poolToNodesMap[tmp.Name].Len() { | ||
return errors.New("nodepool size not match") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
BeforeEach(func() { | ||
By("Start to run nodepool test, cleanup previous resources") | ||
k8sClient = ycfg.YurtE2eCfg.RuntimeClient | ||
poolToNodesMap = map[string]sets.String{} | ||
|
||
util.CleanupNodeLabel(ctx, k8sClient) | ||
util.CleanupNodePool(ctx, k8sClient) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("Cleanup resources after test") | ||
util.CleanupNodeLabel(ctx, k8sClient) | ||
util.CleanupNodePool(ctx, k8sClient) | ||
}) | ||
|
||
It("Test NodePool empty", func() { | ||
By("Run noolpool empty") | ||
Eventually( | ||
func() error { | ||
return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
|
||
Eventually( | ||
func() error { | ||
return checkNodePoolStatus(poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
}) | ||
|
||
It("Test NodePool create", func() { | ||
By("Run nodepool create") | ||
|
||
bjNpName := "beijing" | ||
poolToNodesMap[bjNpName] = sets.NewString("openyurt-e2e-test-worker", "openyurt-e2e-test-worker2") | ||
Eventually( | ||
func() error { | ||
return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
|
||
Eventually( | ||
func() error { | ||
return checkNodePoolStatus(poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
}) | ||
|
||
It("Test NodePool With Multi Nodes", func() { | ||
poolToNodesMap["beijing"] = sets.NewString("openyurt-e2e-test-worker") | ||
poolToNodesMap["hangzhou"] = sets.NewString("openyurt-e2e-test-worker2") | ||
|
||
Eventually( | ||
func() error { | ||
return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
|
||
Eventually( | ||
func() error { | ||
return checkNodePoolStatus(poolToNodesMap) | ||
}, | ||
time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil())) | ||
}) | ||
|
||
}) |
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