Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #70 from sofastack/module-controller-0.3
Browse files Browse the repository at this point in the history
Module controller 0.3
  • Loading branch information
gold300jin authored Sep 4, 2023
2 parents 3b39615 + 4aa416c commit 30202da
Show file tree
Hide file tree
Showing 25 changed files with 1,215 additions and 369 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/module_controller_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
run: go mod download

- name: Test
run: go test -v ./internal/controller
run: make test
2 changes: 1 addition & 1 deletion module-controller/api/v1alpha1/moduledeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type ModuleDeploymentStrategy struct {
type ModuleDeploymentSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
DeploymentName string `json:"deploymentName"`
BaseDeploymentName string `json:"baseDeploymentName"`

Template ModuleTemplateSpec `json:"template,omitempty"`

Expand Down
17 changes: 0 additions & 17 deletions module-controller/api/v1alpha1/well_known_labels.go

This file was deleted.

2 changes: 1 addition & 1 deletion module-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"github.com/sofastack/sofa-serverless/internal/controller"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -32,7 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

moduledeploymentv1alpha1 "github.com/sofastack/sofa-serverless/api/v1alpha1"
"github.com/sofastack/sofa-serverless/internal/controller"
//+kubebuilder:scaffold:imports
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ spec:
spec:
description: ModuleDeploymentSpec defines the desired state of ModuleDeployment
properties:
deployType:
type: string
deploymentName:
BaseDeploymentName:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Important: Run "make" to regenerate code after modifying this file'
type: string
deployType:
type: string
minReadySeconds:
format: int32
type: integer
Expand Down Expand Up @@ -158,8 +158,8 @@ spec:
type: object
type: object
required:
- BaseDeploymentName
- deployType
- deploymentName
type: object
status:
description: ModuleDeploymentStatus defines the observed state of ModuleDeployment
Expand Down
3 changes: 2 additions & 1 deletion module-controller/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ rules:
- list
- patch
- update
- watch
- watch

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
spec:
containers:
- name: dynamic-stock-container
image: serverless-registry.cn-shanghai.cr.aliyuncs.com/opensource/test/dynamic-stock-mng:v0.6
image: serverless-registry.cn-shanghai.cr.aliyuncs.com/opensource/test/dynamic-stock-mng:v0.8
ports:
- containerPort: 8080
- containerPort: 1238
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
app.kubernetes.io/created-by: module-controller
name: moduledeployment-sample
spec:
deploymentName: dynamic-stock-deployment
baseAppName: dynamic-stock-deployment
template:
spec:
module:
Expand Down
3 changes: 3 additions & 0 deletions module-controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.7
github.com/stretchr/testify v1.8.1
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
Expand All @@ -17,6 +18,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
Expand All @@ -42,6 +44,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions module-controller/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
Expand Down
1 change: 1 addition & 0 deletions module-controller/integration-tests/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions module-controller/integration-tests/test_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
141 changes: 141 additions & 0 deletions module-controller/internal/arklet/arklet_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package arklet

import (
"bytes"
"encoding/json"
"fmt"
"github.com/sofastack/sofa-serverless/api/v1alpha1"
"io"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/log"
"strconv"
"sync"
)

const (
DefaultPort = 1238
DefaultInstallPath = "installBiz"
DefaultUninstallPath = "uninstallBiz"
DefaultSwitchPath = "switchBiz"
)

// ArkletClient support install / uninstall /switch biz
type ArkletClient struct {
installPath string

uninstallPath string

switchPath string

port int
}

type ArkletResponseCode string

const (
Success ArkletResponseCode = "SUCCESS"
Failed ArkletResponseCode = "FAILED"
)

type ArkletResponse struct {
// TODO 根据 arklet response 定义再调整
Code ArkletResponseCode

Message string
}

var instance *ArkletClient
var installPath, uninstallPath, switchPath string
var port int
var mockUrl string

var once sync.Once

func init() {
installPath = DefaultInstallPath
uninstallPath = DefaultUninstallPath
switchPath = DefaultSwitchPath
port = DefaultPort
}

func MockClient(customMockUrl string) {
mockUrl = customMockUrl
}

func Client() *ArkletClient {
if instance == nil {
once.Do(func() {
instance = &ArkletClient{installPath: installPath, uninstallPath: uninstallPath, switchPath: switchPath, port: port}
})
}
return instance
}

func (client *ArkletClient) InstallBiz(ip string, moduleInfo v1alpha1.ModuleInfo) (*ArkletResponse, error) {
log.Log.Info("start to install module", "ip", ip, "moduleInfo", moduleInfo)
var url string
if mockUrl != "" {
url = mockUrl
} else {
url = fmt.Sprintf("http://%s:%s/%s", ip, strconv.Itoa(client.port), client.installPath)
}
data := []byte(fmt.Sprintf(`{"bizName": "%s", "bizVersion": "%s", "arkBizFilePath": "%s"}`, moduleInfo.Name, moduleInfo.Version, moduleInfo.Url))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
fmt.Println("Error creating install HTTP request:", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")

httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
fmt.Println("Error sending install HTTP request:", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading install response body:", err)
return nil, err
}
var result ArkletResponse
err = json.Unmarshal(body, &result)
log.Log.Info("install biz success", "result", result, "moduleName", moduleInfo.Name, "url", url, "body", body)
return &result, nil
}

func (client *ArkletClient) UninstallBiz(ip string, moduleName string, moduleVersion string) (*ArkletResponse, error) {
log.Log.Info("start to uninstall module", "ip", ip, "moduleName", moduleName, "moduleVersion", moduleVersion)
var url string
if mockUrl != "" {
url = mockUrl
} else {
url = fmt.Sprintf("http://%s:%s/%s", ip, strconv.Itoa(client.port), client.uninstallPath)
}

data := []byte(fmt.Sprintf(`{"bizName": "%s", "bizVersion": "%s"}`, moduleName, moduleVersion))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
fmt.Println("Error creating uninstall HTTP request:", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")

httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
fmt.Println("Error sending uninstall HTTP request:", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading uninstall response body:", err)
return nil, err
}
var result ArkletResponse
err = json.Unmarshal(body, &result)
log.Log.Info("uninstall biz success", "result", result, "moduleName", moduleName, "moduleVersion", moduleVersion, "url", url, "body", body)
return &result, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package finalizer

const (
ModuleInstalledFinalizer = "serverless.alipay.com/module-installed"

ModuleReplicaSetExistedFinalizer = "serverless.alipay.com/module-replicaset-existed"

ModuleExistedFinalizer = "serverless.alipay.com/module-existed"
)
19 changes: 19 additions & 0 deletions module-controller/internal/constants/label/well_known_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package label

const (
ModuleNameLabel = "serverless.alipay.com/module-name"

ModuleVersionLabel = "serverless.alipay.com/module-version"

BaseInstanceIpLabel = "serverless.alipay.com/base-instance-ip"

BaseInstanceNameLabel = "serverless.alipay.com/base-instance-name"

ModuleReplicasetLabel = "serverless.alipay.com/module-replicaset"

ModuleDeploymentLabel = "serverless.alipay.com/module-deployment"

DeleteModuleLabel = "serverless.alipay.com/delete-module"

ModuleInstanceCount = "serverless.alipay.com/module-instance-count"
)
Loading

0 comments on commit 30202da

Please sign in to comment.