-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add heartbeat for sealos self-host cluster. (#4389)
* feat: add heartbeat for sealos self-host cluster. Signed-off-by: yy <lingdie.yy@outlook.com> * feat: fix imports Signed-off-by: yy <lingdie.yy@outlook.com> * feat: add cluster id logic. Signed-off-by: yy <lingdie.yy@outlook.com> * fix fmt * feat: add heartbeat server side. Signed-off-by: yy <lingdie.yy@outlook.com> * feat: add heartbeat job deploy yaml. Signed-off-by: yy <lingdie.yy@outlook.com> * chore: add license and fix format. Signed-off-by: yy <lingdie.yy@outlook.com> * chore: fix deploy manifest. Signed-off-by: yy <lingdie.yy@outlook.com> * chore: add job/heartbeat to ci. Signed-off-by: yy <lingdie.yy@outlook.com> * chore: fix lint. Signed-off-by: yy <lingdie.yy@outlook.com> --------- Signed-off-by: yy <lingdie.yy@outlook.com>
- Loading branch information
Showing
16 changed files
with
887 additions
and
2 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ use ( | |
./resources | ||
./license | ||
./job/init | ||
./job/heartbeat | ||
./pkg | ||
./objectstorage | ||
) |
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,23 @@ | ||
# Copyright © 2023 sealos. | ||
# | ||
# 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. | ||
|
||
FROM gcr.io/distroless/static:nonroot | ||
ARG TARGETARCH | ||
|
||
WORKDIR / | ||
USER 65532:65532 | ||
|
||
COPY bin/controller-job-heartbeat-$TARGETARCH /heartbeat | ||
|
||
ENTRYPOINT ["/heartbeat"] |
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,86 @@ | ||
|
||
# Image URL to use all building/pushing image targets | ||
IMG ?= ghcr.io/labring/sealos-job-heartbeat-controller:latest | ||
TARGETARCH ?= amd64 | ||
GOARCH ?= amd64 | ||
|
||
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) | ||
ifeq (,$(shell go env GOBIN)) | ||
GOBIN=$(shell go env GOPATH)/bin | ||
else | ||
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
# Setting SHELL to bash allows bash commands to be executed by recipes. | ||
# Options are set to exit when a recipe line exits non-zero or a piped command fails. | ||
SHELL = /usr/bin/env bash -o pipefail | ||
.SHELLFLAGS = -ec | ||
|
||
.PHONY: all | ||
all: build | ||
|
||
##@ General | ||
|
||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
.PHONY: help | ||
help: ## Display this help. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
.PHONY: fmt | ||
fmt: ## Run go fmt against code. | ||
go fmt ./... | ||
|
||
.PHONY: vet | ||
vet: ## Run go vet against code. | ||
go vet ./... | ||
|
||
##@ Build | ||
|
||
.PHONY: build | ||
build: fmt vet ## Build manager binary. | ||
CGO_ENABLED=0 GOOS=linux go build -o bin/heartbeat-${GOARCH} cmd/main.go && chmod +x bin/heartbeat-${GOARCH} && cp bin/heartbeat-${GOARCH} bin/manager | ||
|
||
|
||
.PHONY: run | ||
run: fmt vet ## Run a controller from your host. | ||
go run ./cmd/main.go | ||
|
||
# If you wish built the manager image targeting other platforms you can use the --platform flag. | ||
# (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/ | ||
.PHONY: docker-build | ||
docker-build: ## Build docker image with the manager. | ||
mv bin/heartbeat-${TARGETARCH} bin/controller-job-heartbeat-${TARGETARCH} || true | ||
chmod +x bin/controller-job-heartbeat-${TARGETARCH} | ||
docker build -t ${IMG} . --build-arg TARGETARCH=${TARGETARCH} | ||
|
||
.PHONY: docker-push | ||
docker-push: ## Push docker image with the manager. | ||
docker push ${IMG} | ||
|
||
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple | ||
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: | ||
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/ | ||
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/ | ||
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail) | ||
# To properly provided solutions that supports more than one platform you should use this option. | ||
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le | ||
.PHONY: docker-buildx | ||
docker-buildx: test ## Build and push docker image for the manager for cross-platform support | ||
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile | ||
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross | ||
- docker buildx create --name project-v3-builder | ||
docker buildx use project-v3-builder | ||
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . | ||
- docker buildx rm project-v3-builder | ||
rm Dockerfile.cross |
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,26 @@ | ||
// Copyright © 2023 sealos. | ||
// | ||
// 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 v1alpha | ||
|
||
type ClusterResource struct { | ||
Node int64 `json:"node"` | ||
CPU int64 `json:"cpu"` | ||
Mem int64 `json:"mem"` | ||
} | ||
|
||
type Request struct { | ||
ClusterID string `json:"clusterID"` | ||
ClusterResource *ClusterResource `json:"clusterResource"` | ||
} |
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,53 @@ | ||
// Copyright © 2023 sealos. | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"github.com/go-resty/resty/v2" | ||
|
||
"github.com/labring/sealos/controllers/job/heartbeat/api/v1alpha" | ||
"github.com/labring/sealos/controllers/job/heartbeat/internal/cluster" | ||
"github.com/labring/sealos/controllers/pkg/utils/logger" | ||
) | ||
|
||
func main() { | ||
resource, err := cluster.GetClusterResources() | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
logger.Info("cluster resource: %+v", resource) | ||
|
||
clusterID, err := cluster.GetClusterID() | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
logger.Info("cluster id: %s", clusterID) | ||
|
||
client := resty.New() | ||
_, err = client.R(). | ||
SetHeader("Content-Type", "application/json"). | ||
SetBody(v1alpha.Request{ | ||
ClusterID: clusterID, | ||
ClusterResource: resource, | ||
}). | ||
Post("https://license.sealos.io/api/v1alpha/heartbeat/cluster") | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
return | ||
} | ||
logger.Info("heartbeat success") | ||
} |
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,8 @@ | ||
FROM scratch | ||
|
||
USER 65532:65532 | ||
|
||
COPY registry registry | ||
COPY manifests manifests | ||
|
||
CMD ["kubectl apply -f manifests"] |
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,68 @@ | ||
# Copyright © 2023 sealos. | ||
# | ||
# 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. | ||
|
||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: heartbeat-cronjob | ||
namespace: sealos | ||
spec: | ||
schedule: "0 0 * * *" | ||
jobTemplate: | ||
spec: | ||
activeDeadlineSeconds: 600 | ||
backoffLimit: 1 | ||
template: | ||
spec: | ||
restartPolicy: Never | ||
containers: | ||
- name: heartbeat | ||
image: ghcr.io/labring/sealos-job-heartbeat-controller:latest | ||
securityContext: | ||
allowPrivilegeEscalation: false | ||
serviceAccountName: heartbeat-cronjob | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: heartbeat-cronjob | ||
namespace: sealos | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
name: heartbeat-cronjob-role | ||
namespace: sealos | ||
rules: | ||
- apiGroups: [ "" ] | ||
resources: [ "nodes" ] | ||
verbs: [ "get" ] | ||
- apiGroups: [ "" ] | ||
resources: [ "namespaces" ] | ||
resourceNames: [ "kube-system" ] | ||
verbs: [ "get" ] | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: heartbeat-cronjob-role-binding | ||
namespace: sealos | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: Role | ||
name: heartbeat-cronjob-role | ||
subjects: | ||
- kind: ServiceAccount | ||
name: heartbeat-cronjob | ||
namespace: sealos |
Oops, something went wrong.