This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from sofastack/module-controller-0.3
Module controller 0.3
- Loading branch information
Showing
25 changed files
with
1,215 additions
and
369 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ jobs: | |
run: go mod download | ||
|
||
- name: Test | ||
run: go test -v ./internal/controller | ||
run: make test |
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 was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -100,4 +100,5 @@ rules: | |
- list | ||
- patch | ||
- update | ||
- watch | ||
- watch | ||
|
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
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
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 @@ | ||
package main |
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 @@ | ||
package main |
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 @@ | ||
package main |
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 @@ | ||
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
module-controller/internal/constants/finalizer/well_known_finalizers.go
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,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
19
module-controller/internal/constants/label/well_known_labels.go
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,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" | ||
) |
Oops, something went wrong.