Skip to content

Commit

Permalink
feat: helm deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Oct 4, 2023
1 parent 7a96a08 commit 3c30ea0
Show file tree
Hide file tree
Showing 21 changed files with 681 additions and 160 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
## podDeployer-operator
### pod按照容器权重顺序启动operator
## podDeployer-operator 按照容器权重顺序启动 operator
![]()
### 有重大 bug! patch 不能保证容器顺序启动

### 项目思路与设计
设计背景:
集群在部署服务时,可能会需要多个container部署在同一个pod中的场景。当不同的container有顺序需要依赖时,可以采用k8s 提供的hook来做命令脚本
集群在部署服务时,可能会需要多个 container 部署在同一个 pod 中的场景。当不同的 container 有顺序需要依赖时,可以采用 k8s 提供的 hook 来做命令脚本,也能使用 patch 进行操作。

思路:对deployment使用clientSet patch操作实现
思路:对 deployment daemonset statefulset 使用 clientSet patch 操作实现

### 项目功能

- 使用 patch 操作实现容器顺序启动功能(仅仅用于模拟)。 可参考 [deployment yaml参考](yaml/example_deployment.yaml) [statefulset yaml参考](yaml/example_statefulset.yaml) [daemonset yaml参考](yaml/example_daemonset.yaml)
```yaml
apiVersion: api.practice.com/v1alpha1
kind: Poddeployer
metadata:
name: mypoddeployer
name: mypoddeployer-deployment
namespace: default
spec:
type: apps/v1/deployments # 可选择使用哪种类型,目前支持三种:apps/v1/deployments apps/v1/statefulsets apps/v1/daemonsets
# 资源对象的原生 Spec
# 目前支持三种:deployment_spec statefulset_spec daemonset_spec
deployment_spec:
replicas: 1
template:
Expand Down Expand Up @@ -47,5 +51,4 @@ spec:
value: 100
- image: example4
value: 1000

```
23 changes: 23 additions & 0 deletions helm/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: helm
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
50 changes: 50 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
namespace: {{ .Values.namespace }}
labels:
app: {{ .Values.name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.name }}
template:
metadata:
labels:
app: {{ .Values.name }}
spec:
serviceAccountName: {{ .Values.serviceAccountName }}
nodeName: vm-0-16-centos
containers:
- name: {{ .Values.name }}
image: "{{ .Values.image }}"
imagePullPolicy: IfNotPresent
env:
- name: "Release"
value: "1"
workingDir: "/app"
command: [ "./mypoddeployeroperator" ]
{{- if .Values.service.ports }}
ports:
{{- range .Values.service.ports }}
- name: port-80
containerPort: 80
protocol: TCP
{{- end }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
21 changes: 21 additions & 0 deletions helm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.serviceName }}
namespace: {{ .Values.namespace }}
labels:
app: {{ .Values.name }}
spec:
type: {{ .Values.service.type }}
{{- if .Values.service.ports }}
ports:
{{- range .Values.service.ports }}
- port: {{ .port }}
targetPort: {{ .port }}
nodePort: {{ .nodePort }}
protocol: TCP
name: port-{{ .port }}
{{- end }}
{{- end }}
selector:
app: {{ .Values.name }}
68 changes: 68 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Default values for helm.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1
name: mypoddeployer-controller
namespace: default
serviceName: mypoddeployer-svc
serviceAccountName: mypoddeployer-sa
image: poddeployeroperator:v1


imagePullSecrets: []
nameOverride: ""

serviceAccount:
# Specifies whether a service account should be created
create: true
# Annotations to add to the service account
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: ""

podAnnotations: {}

podSecurityContext: {}
# fsGroup: 2000

securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

service:
type: NodePort
ports:
- port: 8888
nodePort: 31131


resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80

nodeSelector: {}

tolerations: []

affinity: {}
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/myoperator/poddeployer/pkg/controller"
"github.com/myoperator/poddeployer/pkg/k8sconfig"
v1 "k8s.io/api/apps/v1"
v12 "k8s.io/api/core/v1"
_ "k8s.io/code-generator"
"k8s.io/klog/v2"
"log"
Expand Down Expand Up @@ -32,7 +33,7 @@ func main() {
var d time.Duration = 0
// 1. 管理器初始化
mgr, err := manager.New(k8sconfig.K8sRestConfig(), manager.Options{
Logger: logf.Log.WithName("poddeployer-operator"),
Logger: logf.Log.WithName("poddeployer-operator"),
SyncPeriod: &d, // resync不设置触发
})
if err != nil {
Expand All @@ -48,16 +49,19 @@ func main() {
}

// 3. 控制器相关
podReStarterCtl := controller.NewPodDeployerController()
podReStarterCtl := controller.NewPodDeployerController(k8sconfig.DynamicClient)

err = builder.ControllerManagedBy(mgr).
For(&podrestarterv1alpha1.Poddeployer{}).
Watches(&source.Kind{Type: &v1.Deployment{}},
handler.Funcs{
DeleteFunc: podReStarterCtl.DeploymentDeleteHandler,
},
).
Complete(podReStarterCtl)
).Watches(&source.Kind{Type: &v12.Pod{}},
handler.Funcs{
DeleteFunc: podReStarterCtl.PodDeleteHandler,
},
).Complete(podReStarterCtl)

errC := make(chan error)

Expand Down
7 changes: 5 additions & 2 deletions pkg/apis/podDeployer/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ type Poddeployer struct {
}

type PodDeployerSpec struct {
DeploymentSpec appsv1.DeploymentSpec `json:"deployment_spec"`
PriorityImages []PriorityImage `json:"priority_images,omitempty"`
Type string `json:"type"`
DaemonSetSpec appsv1.DaemonSetSpec `json:"daemonset_spec,omitempty"`
StatefulSetSpec appsv1.StatefulSetSpec `json:"statefulset_spec,omitempty"`
DeploymentSpec appsv1.DeploymentSpec `json:"deployment_spec,omitempty"`
PriorityImages []PriorityImage `json:"priority_images,omitempty"`
}

type PriorityImage struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/podDeployer/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3c30ea0

Please sign in to comment.