l-86 插曲-镜像运行

master
dongming 2 years ago
parent a9f6789070
commit 5ce16fc777

@ -3,6 +3,8 @@ FROM golang:1.19 as builder
ARG TARGETOS ARG TARGETOS
ARG TARGETARCH ARG TARGETARCH
ENV GOPROXY=https://goproxy.cn
WORKDIR /workspace WORKDIR /workspace
# Copy the Go Modules manifests # Copy the Go Modules manifests
COPY go.mod go.mod COPY go.mod go.mod
@ -28,6 +30,8 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o ma
FROM gcr.io/distroless/static:nonroot FROM gcr.io/distroless/static:nonroot
WORKDIR / WORKDIR /
COPY --from=builder /workspace/manager . COPY --from=builder /workspace/manager .
COPY controllers/ controllers/
USER 65532:65532 USER 65532:65532
ENTRYPOINT ["/manager"] ENTRYPOINT ["/manager"]

@ -58,6 +58,10 @@ vet: ## Run go vet against code.
test: manifests generate fmt vet envtest ## Run tests. test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
.PHONY: e2e-test
e2e-test:
cd test/e2e && go test -tags=e2e --config config.yaml -startup-timeout 36000 ./...
##@ Build ##@ Build
.PHONY: build .PHONY: build
@ -72,9 +76,13 @@ run: manifests generate fmt vet ## Run a controller from your host.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build .PHONY: docker-build
docker-build: test ## Build docker image with the manager. docker-build: ## test ## Build docker image with the manager.
docker build -t ${IMG} . docker build -t ${IMG} .
.PHONY: docker-load
docker-load:
kind load docker-image ${IMG} --name e2e
.PHONY: docker-push .PHONY: docker-push
docker-push: ## Push docker image with the manager. docker-push: ## Push docker image with the manager.
docker push ${IMG} docker push ${IMG}
@ -153,4 +161,8 @@ $(ENVTEST): $(LOCALBIN)
.PHONY: wait-dep .PHONY: wait-dep
wait-dep: wait-dep:
kubectl -n ingress-nginx rollout status deploy/ingress-nginx-controller -w --timeout=40m || kubectl -n ingress-nginx get pod -o wide kubectl -n ingress-nginx rollout status deploy/ingress-nginx-controller -w --timeout=40m
.PHONY: wait-deploy
wait-deploy:
kubectl -n mashibing-deployment-system rollout status deploy/mashibing-deployment-controller-manager -w --timeout=40m

@ -31,7 +31,7 @@ spec:
capabilities: capabilities:
drop: drop:
- "ALL" - "ALL"
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 image: localhost:5001/kubebuilder/kube-rbac-proxy:v0.13.0
args: args:
- "--secure-listen-address=0.0.0.0:8443" - "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8080/" - "--upstream=http://127.0.0.1:8080/"

@ -1,2 +1,8 @@
resources: resources:
- manager.yaml - manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: mashibing.com/deployment/msbdeployment
newTag: v0.0.1

@ -5,6 +5,30 @@ metadata:
creationTimestamp: null creationTimestamp: null
name: manager-role name: manager-role
rules: rules:
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- deployments
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups: - apiGroups:
- apps.mashibing.com - apps.mashibing.com
resources: resources:
@ -31,3 +55,15 @@ rules:
- get - get
- patch - patch
- update - update
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- create
- delete
- get
- list
- patch
- update
- watch

@ -48,6 +48,9 @@ type MsbDeploymentReconciler struct {
//+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments/status,verbs=get;update;patch //+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments/finalizers,verbs=update //+kubebuilder:rbac:groups=apps.mashibing.com,resources=msbdeployments/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="networking.k8s.io",resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to // Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state. // move the current state of the cluster closer to the desired state.
@ -287,7 +290,7 @@ func (r *MsbDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr). return ctrl.NewControllerManagedBy(mgr).
For(&myAppsv1.MsbDeployment{}). For(&myAppsv1.MsbDeployment{}).
Owns(&appsv1.Deployment{}). // 监控 deployment 类型,变更就触发 reconciler Owns(&appsv1.Deployment{}). // 监控 deployment 类型,变更就触发 reconciler
Owns(&corev1.Service{}). // 监控 service 类型,变更就触发 reconciler Owns(&corev1.Service{}). // 监控 service 类型,变更就触发 reconciler
Owns(&networkv1.Ingress{}). // 监控 ingress 类型,变更就触发 reconciler Owns(&networkv1.Ingress{}). // 监控 ingress 类型,变更就触发 reconciler
Complete(r) Complete(r)
} }

@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuite name="e2e" tests="0" failures="0" errors="0" time="434.56"></testsuite> <testsuite name="e2e" tests="0" failures="0" errors="0" time="446.979"></testsuite>

@ -51,3 +51,44 @@ install:
- wait-dep - wait-dep
path: ../.. path: ../..
ignoreFail: false ignoreFail: false
- name: docker-build
cmd: make
args:
- docker-build
- IMG=mashibing.com/deployment/msbdeployment:v0.0.1
path: ../..
ignoreFail: false
- name: docker-load
cmd: make
args:
- docker-load
- IMG=mashibing.com/deployment/msbdeployment:v0.0.1
path: ../..
ignoreFail: false
- name: install-crd
cmd: make
args:
- install
path: ../..
ignoreFail: false
- name: deploy
cmd: make
args:
- deploy
- IMG=mashibing.com/deployment/msbdeployment:v0.0.1
path: ../..
ignoreFail: false
- name: wait-deploy
cmd: make
args:
- wait-deploy
path: ../..
ignoreFail: false
- name: check
cmd: kubectl
args:
- get
- pod
- -A
path: ../..
ignoreFail: false

@ -0,0 +1,78 @@
package create
import (
"context"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"time"
"mashibing.com/pkg/mashibing-deployment/test/framework"
)
// 真正的测试函数
// 测试创建Ingress模式
func CreateIngressMsbDeployment(ctx *framework.TestContext, f *framework.Framework) {
var (
// 1. 准备测试数据
ctFilePath = "create/testdata/create-ingress.yaml"
obj = &unstructured.Unstructured{Object: make(map[string]interface{})}
dc dynamic.Interface
cs *kubernetes.Clientset
// 3. 准备测试用到的全局变量
msbGVR = schema.GroupVersionResource{
Group: "apps.mashibing.com",
Version: "v1",
Resource: "msbdeployments",
}
err error
)
BeforeEach(func() {
// 2. 加载测试数据
err = f.LoadYamlToUnstructured(ctFilePath, obj)
Expect(err).Should(BeNil())
// 4. 初始化测试用到的全局变量
dc = ctx.CreateDynamicClient()
cs = ctx.CreateClientSet()
})
Context("Create msbdeployment mod ingress", func() {
It("Should be create mod ingress success", func() {
_, err = dc.Resource(msbGVR).Namespace("default").Create(context.TODO(), obj, metav1.CreateOptions{})
Expect(err).Should(BeNil())
By("Sleep 1 second wait creating done")
time.Sleep(time.Second)
})
It("Should be exist msbdeployment", func() {
_, err = dc.Resource(msbGVR).Namespace("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
Expect(err).Should(BeNil())
})
It("Should be exist deployment", func() {
_, err = cs.AppsV1().Deployments("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
Expect(err).Should(BeNil())
})
It("Should be exist service", func() {
_, err = cs.CoreV1().Services("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
Expect(err).Should(BeNil())
})
It("Should be exist ingress", func() {
_, err = cs.NetworkingV1().Ingresses("default").Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
Expect(err).Should(BeNil())
})
})
Context("Delete msbdeployment mod ingress", func() {
It("Should be delete mod ingress success", func() {})
It("Should not be exist msbdeployment", func() {})
It("Should not be exist deployment", func() {})
It("Should not be exist service", func() {})
It("Should not be exist ingress", func() {})
})
}

@ -0,0 +1,28 @@
package create
import (
. "github.com/onsi/ginkgo"
"mashibing.com/pkg/mashibing-deployment/test/framework"
)
// 真正的测试函数
// 测试创建Nodeport模式
func CreateNodeportMsbDeployment(ctx *framework.TestContext, f *framework.Framework) {
Context("Create msbdeployment mod nodeport", func() {
It("Should be create mode nodeport success", func() {})
It("Should be exist msbdeployment", func() {})
It("Should be exist deployment", func() {})
It("Should be exist service", func() {})
It("Should not be exist ingress", func() {})
})
Context("Delete msbdeployment mod nodeport", func() {
It("Should be delete mod nodeport success", func() {})
It("Should not be exist msbdeployment", func() {})
It("Should not be exist deployment", func() {})
It("Should not be exist service", func() {})
It("Should not be exist ingress", func() {})
})
}

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: create-ingress
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: ingress
ingressDomain: www.mashingbing-test.com

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: create-nodeport
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: nodeport
nodePort: 30000

@ -0,0 +1,9 @@
//go:build e2e
// +build e2e
package e2e
import "mashibing.com/pkg/mashibing-deployment/test/e2e/create"
var _ = fmw.Describe("Create msbdeployment mod ingress", create.CreateIngressMsbDeployment)
var _ = fmw.Describe("Create msbdeployment mod nodeport", create.CreateNodeportMsbDeployment)

@ -1,2 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuite name="e2e" tests="0" failures="0" errors="0" time="53.339"></testsuite> <testsuite name="e2e" tests="44" failures="0" errors="0" time="88.641">
<testcase name="Create msbdeployment mod ingress Create msbdeployment mod ingress Should be create mod ingress success" classname="e2e" time="1.022819158"></testcase>
<testcase name="Create msbdeployment mod ingress Create msbdeployment mod ingress Should be exist msbdeployment" classname="e2e" time="0.011042164"></testcase>
<testcase name="Create msbdeployment mod ingress Create msbdeployment mod ingress Should be exist deployment" classname="e2e" time="0.010476651"></testcase>
<testcase name="Create msbdeployment mod ingress Create msbdeployment mod ingress Should be exist service" classname="e2e" time="0.011884409"></testcase>
<testcase name="Create msbdeployment mod ingress Create msbdeployment mod ingress Should be exist ingress" classname="e2e" time="0.007711314"></testcase>
<testcase name="Create msbdeployment mod ingress Delete msbdeployment mod ingress Should be delete mod ingress success" classname="e2e" time="0.159769005"></testcase>
<testcase name="Create msbdeployment mod ingress Delete msbdeployment mod ingress Should not be exist msbdeployment" classname="e2e" time="0.406098839"></testcase>
<testcase name="Create msbdeployment mod ingress Delete msbdeployment mod ingress Should not be exist deployment" classname="e2e" time="0.406216596"></testcase>
<testcase name="Create msbdeployment mod ingress Delete msbdeployment mod ingress Should not be exist service" classname="e2e" time="0.386941655"></testcase>
<testcase name="Create msbdeployment mod ingress Delete msbdeployment mod ingress Should not be exist ingress" classname="e2e" time="0.400914807"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be create mod ingress success" classname="e2e" time="0.398575828"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be exist msbdeployment" classname="e2e" time="0.40035368"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be exist deployment" classname="e2e" time="0.402275948"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be exist service" classname="e2e" time="0.397884956"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be exist ingress" classname="e2e" time="0.400241593"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should be update to nodeport success" classname="e2e" time="0.400317655"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Update msbdeployment mod ingress to nodeport Should not be exist ingress" classname="e2e" time="0.400142585"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Delete msbdeployment i2n Should be delete success" classname="e2e" time="0.404196039"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Delete msbdeployment i2n Should not be exist msbdeployment" classname="e2e" time="0.394759408"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Delete msbdeployment i2n Should not be exist deployment" classname="e2e" time="0.400598306"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Delete msbdeployment i2n Should not be exist service" classname="e2e" time="0.399921651"></testcase>
<testcase name="Update msbdeployment mod ingress to Nodeport Delete msbdeployment i2n Should not be exist ingress" classname="e2e" time="0.399307785"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be create mod nodeport success" classname="e2e" time="0.400627036"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be exist msbdeployment" classname="e2e" time="0.399831122"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be exist deployment" classname="e2e" time="0.399952299"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be exist service" classname="e2e" time="0.3998821"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should not be exist ingress" classname="e2e" time="0.40065962"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be update to ingress success" classname="e2e" time="0.399780321"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Update msbdeployment mod nodeport to ingress Should be exist ingress" classname="e2e" time="0.399753287"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Delete msbdeployment n2i Should be delete success" classname="e2e" time="0.400256732"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Delete msbdeployment n2i Should not be exist msbdeployment" classname="e2e" time="0.399516085"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Delete msbdeployment n2i Should not be exist deployment" classname="e2e" time="0.400321443"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Delete msbdeployment n2i Should not be exist service" classname="e2e" time="0.39949787"></testcase>
<testcase name="Update msbdeployment mod nodeport to Ingress Delete msbdeployment n2i Should not be exist ingress" classname="e2e" time="0.399721876"></testcase>
<testcase name="Create msbdeployment mod nodeport Create msbdeployment mod nodeport Should be create mode nodeport success" classname="e2e" time="0.400345032"></testcase>
<testcase name="Create msbdeployment mod nodeport Create msbdeployment mod nodeport Should be exist msbdeployment" classname="e2e" time="0.399825314"></testcase>
<testcase name="Create msbdeployment mod nodeport Create msbdeployment mod nodeport Should be exist deployment" classname="e2e" time="0.399686453"></testcase>
<testcase name="Create msbdeployment mod nodeport Create msbdeployment mod nodeport Should be exist service" classname="e2e" time="0.400756069"></testcase>
<testcase name="Create msbdeployment mod nodeport Create msbdeployment mod nodeport Should not be exist ingress" classname="e2e" time="0.4001103"></testcase>
<testcase name="Create msbdeployment mod nodeport Delete msbdeployment mod nodeport Should be delete mod nodeport success" classname="e2e" time="0.398722227"></testcase>
<testcase name="Create msbdeployment mod nodeport Delete msbdeployment mod nodeport Should not be exist msbdeployment" classname="e2e" time="0.401853646"></testcase>
<testcase name="Create msbdeployment mod nodeport Delete msbdeployment mod nodeport Should not be exist deployment" classname="e2e" time="0.403237009"></testcase>
<testcase name="Create msbdeployment mod nodeport Delete msbdeployment mod nodeport Should not be exist service" classname="e2e" time="0.398816187"></testcase>
<testcase name="Create msbdeployment mod nodeport Delete msbdeployment mod nodeport Should not be exist ingress" classname="e2e" time="0.397479371"></testcase>
</testsuite>

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: update-ingress
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: nodeport
nodePort: 30000

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: update-ingress
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: ingress
ingressDomain: www.mashingbing-test.com

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: update-nodeport
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: ingress
ingressDomain: www.mashingbing-test.com

@ -0,0 +1,11 @@
apiVersion: apps.mashibing.com/v1
kind: MsbDeployment
metadata:
name: update-nodeport
spec:
image: nginx
port: 80
replicas: 2
expose:
mode: nodeport
nodePort: 30000

@ -0,0 +1,31 @@
package update
import (
. "github.com/onsi/ginkgo"
"mashibing.com/pkg/mashibing-deployment/test/framework"
)
// 真正的测试函数
// 测试从Ingress模式更新为Nodeport模式
func UpdateI2NMsbDeployment(ctx *framework.TestContext, f *framework.Framework) {
Context("Update msbdeployment mod ingress to nodeport", func() {
It("Should be create mod ingress success", func() {})
It("Should be exist msbdeployment", func() {})
It("Should be exist deployment", func() {})
It("Should be exist service", func() {})
It("Should be exist ingress", func() {})
It("Should be update to nodeport success", func() {})
It("Should not be exist ingress", func() {})
})
Context("Delete msbdeployment i2n", func() {
It("Should be delete success", func() {})
It("Should not be exist msbdeployment", func() {})
It("Should not be exist deployment", func() {})
It("Should not be exist service", func() {})
It("Should not be exist ingress", func() {})
})
}

@ -0,0 +1,31 @@
package update
import (
. "github.com/onsi/ginkgo"
"mashibing.com/pkg/mashibing-deployment/test/framework"
)
// 真正的测试函数
// 测试从Nodeport模式更新为Ingress模式
func UpdateN2IMsbDeployment(ctx *framework.TestContext, f *framework.Framework) {
Context("Update msbdeployment mod nodeport to ingress", func() {
It("Should be create mod nodeport success", func() {})
It("Should be exist msbdeployment", func() {})
It("Should be exist deployment", func() {})
It("Should be exist service", func() {})
It("Should not be exist ingress", func() {})
It("Should be update to ingress success", func() {})
It("Should be exist ingress", func() {})
})
Context("Delete msbdeployment n2i", func() {
It("Should be delete success", func() {})
It("Should not be exist msbdeployment", func() {})
It("Should not be exist deployment", func() {})
It("Should not be exist service", func() {})
It("Should not be exist ingress", func() {})
})
}

@ -0,0 +1,9 @@
//go:build e2e
// +build e2e
package e2e
import "mashibing.com/pkg/mashibing-deployment/test/e2e/update"
var _ = fmw.Describe("Update msbdeployment mod ingress to Nodeport", update.UpdateI2NMsbDeployment)
var _ = fmw.Describe("Update msbdeployment mod nodeport to Ingress", update.UpdateN2IMsbDeployment)

@ -5,12 +5,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"math/rand" "math/rand"
"os" "os"
"regexp" "regexp"
@ -21,6 +15,14 @@ import (
"github.com/onsi/ginkgo" "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters" "github.com/onsi/ginkgo/reporters"
"github.com/onsi/gomega" "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
) )
var DefaultStartTimeout = float64(60 * 60) var DefaultStartTimeout = float64(60 * 60)
@ -199,6 +201,17 @@ func (f *Framework) DestroyTestEnvironment() error {
} }
// 加载测试文件内容到对象中
func (f *Framework) LoadYamlToUnstructured(ctFilePath string, obj *unstructured.Unstructured) error {
data, err := os.ReadFile(ctFilePath)
if err != nil {
return err
}
//var o = map[string]interface{}{}
//yaml.Unmarshal(data, &o)
return yaml.Unmarshal(data, &(obj.Object))
}
func (f *Framework) Describe(name string, ctxFunc ContextFunc) bool { func (f *Framework) Describe(name string, ctxFunc ContextFunc) bool {
// 整个函数,实际上是调用 ginkgo的Describe // 整个函数,实际上是调用 ginkgo的Describe
return ginkgo.Describe(name, func() { return ginkgo.Describe(name, func() {
@ -235,10 +248,7 @@ func (f *Framework) createTestContext(name string, nsCreate bool) (TestContext,
tc := TestContext{} tc := TestContext{}
// 2. 检查 f 是否为空 // 2. 检查 f 是否为空
if f.Config == nil || f.ClusterConfig == nil { if f.Config == nil || f.ClusterConfig == nil {
return tc, field.Invalid( return tc, nil
field.NewPath("config/clusterConfig"),
nil,
"Not inital config object")
} }
// 3. 填充字段 // 3. 填充字段
@ -300,5 +310,4 @@ func (f *Framework) deleteTestContext(ctx TestContext) error {
// 如果执行过程中出现错误同样使用errs = append(errs, err)来追加错误 // 如果执行过程中出现错误同样使用errs = append(errs, err)来追加错误
return errs.ToAggregate() return errs.ToAggregate()
} }

@ -1,6 +1,12 @@
package framework package framework
import "k8s.io/client-go/rest" import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// 1. 定义一个测试的入口函数Describe这里接收测试的描述以及contextFunc // 1. 定义一个测试的入口函数Describe这里接收测试的描述以及contextFunc
// 1.1 这里边会调用context创建方法来创建context // 1.1 这里边会调用context创建方法来创建context
@ -17,3 +23,27 @@ type TestContext struct {
} }
type ContextFunc func(ctx *TestContext, f *Framework) type ContextFunc func(ctx *TestContext, f *Framework)
// 如果不用动态的client那么我们访问这些资源的时候就需要
// 1. 自己创建rest api 的请求
// 2. 获取对应资源的 client sdk
// CreateDynamicClient 创建动态的 client用来访问自定定义或者后安装的资源
func (tc *TestContext) CreateDynamicClient() dynamic.Interface {
By("Create a Dynamic Client")
c, err := dynamic.NewForConfig(tc.Config)
if err != nil {
Expect(err).Should(BeNil())
}
return c
}
// 创建一个 clientset用来访问内置资源
func (tc *TestContext) CreateClientSet() *kubernetes.Clientset {
By("Create a ClientSet client")
c, err := kubernetes.NewForConfig(tc.Config)
if err != nil {
Expect(err).Should(BeNil())
}
return c
}

Loading…
Cancel
Save