Skip to content

Commit 7eaa9f4

Browse files
Merge pull request #179 from stoneshi-yunify/master
add s2i config
2 parents 2363730 + f08fa17 commit 7eaa9f4

File tree

9 files changed

+121
-46
lines changed

9 files changed

+121
-46
lines changed

OWNERS

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
approvers:
2-
- shaowenchen
3-
- linuxsuren
2+
- stoneshi-yunify
3+
- renyunkang
44

55
reviewers:
6-
- magicsong
7-
- runzexia
8-
- shaowenchen
9-
- JohnNiang
6+
- stoneshi-yunify
7+
- renyunkang

README.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,9 @@ Compare with native S2I, S2I Operator also has the following advantages on the o
1616

1717
## Installation
1818

19-
#### Prerequisites
20-
21-
1. A Kubernetes cluster. (if you don't have an existing cluster, please [create it](https://kubernetes.io/docs/setup/).
22-
2. Grant cluster-admin permissions to the current user.
23-
24-
#### Install S2I Operator
25-
26-
You can install S2I Operator in any kubernetes cluster with following commands:
27-
28-
```shell
29-
# create a namespaces, such as kubesphere-devops-system
30-
kubectl create ns kubesphere-devops-system
31-
# create S2I Operator and all CRD
32-
kubectl apply -f https://github.com/kubesphere/s2ioperator/releases/download/v0.0.2/s2ioperator.yaml
33-
```
34-
35-
Now monitor the S2I Operator components show a `STATUS` of `Running`:
36-
37-
```shell
38-
# please change you namespace
39-
kubectl -n kubesphere-devops-system get pods -w
40-
```
19+
For better experience, please install S2I Operator via following steps:
20+
1. Refer to https://kubesphere.io and install latest KubeSphere on your cluster.
21+
2. Login KubeSphere console, open the 'KubeSphere Marketplace', and install the `Image Builder` extension.
4122

4223
## Quick Start
4324

cmd/manager/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/kubesphere/s2ioperator/pkg/apis"
2424
"github.com/kubesphere/s2ioperator/pkg/apis/devops/v1alpha1"
25+
s2iconfig "github.com/kubesphere/s2ioperator/pkg/config"
2526
"github.com/kubesphere/s2ioperator/pkg/controller"
2627
"github.com/kubesphere/s2ioperator/pkg/handler"
2728
"github.com/kubesphere/s2ioperator/pkg/metrics"
@@ -41,10 +42,21 @@ func init() {
4142

4243
func main() {
4344
var metricsAddr string
45+
var s2iRunJobTemplatePath string // checkout config/templates/s2irun-template.yaml for example
4446
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
47+
flag.StringVar(&s2iRunJobTemplatePath, "s2irun-job-template", "/etc/template/job.yaml", "the s2irun job template file path")
4548
flag.Parse()
4649
log := ctrl.Log.WithName("entrypoint")
4750

51+
jobTemplateData, err := os.ReadFile(s2iRunJobTemplatePath)
52+
if err != nil {
53+
log.Error(err, "failed to read s2irun template file", "filename", s2iRunJobTemplatePath)
54+
os.Exit(1)
55+
}
56+
s2iConfig := &s2iconfig.Config{
57+
S2IRunJobTemplate: jobTemplateData,
58+
}
59+
4860
// Get a config to talk to the apiserver
4961
log.Info("setting up client for manager")
5062
cfg, err := config.GetConfig()
@@ -78,7 +90,7 @@ func main() {
7890

7991
// Setup all Controllers
8092
log.Info("Setting up controller")
81-
if err := controller.AddToManager(mgr); err != nil {
93+
if err := controller.AddToManager(mgr, s2iConfig); err != nil {
8294
log.Error(err, "unable to register controllers to the manager")
8395
os.Exit(1)
8496
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: s2irun-template
5+
data:
6+
job.yaml: |
7+
apiVersion: batch/v1
8+
kind: Job
9+
metadata:
10+
name: {{.ObjectMetaName}}
11+
namespace: {{.ObjectMetaNamespace}}
12+
spec:
13+
backoffLimit: {{.SpecBackoffLimit}}
14+
template:
15+
metadata:
16+
labels:
17+
job-name: {{.SpecTemplateObjectMetaLabelJobName}}
18+
spec:
19+
affinity:
20+
nodeAffinity:
21+
preferredDuringSchedulingIgnoredDuringExecution:
22+
- preference:
23+
matchExpressions:
24+
- key: node-role.kubernetes.io/worker
25+
operator: In
26+
values:
27+
- ci
28+
weight: 1
29+
containers:
30+
- env:
31+
- name: S2I_CONFIG_PATH
32+
value: /etc/data/config.json
33+
- name: POD_NAMESPACE
34+
valueFrom:
35+
fieldRef:
36+
apiVersion: v1
37+
fieldPath: metadata.namespace
38+
- name: POD_NAME
39+
valueFrom:
40+
fieldRef:
41+
apiVersion: v1
42+
fieldPath: metadata.name
43+
image: {{.ContainerS2IRunImage}}
44+
imagePullPolicy: IfNotPresent
45+
name: s2irun
46+
volumeMounts:
47+
- mountPath: /etc/data
48+
name: config-data
49+
readOnly: true
50+
- mountPath: /var/run/docker.sock
51+
name: docker-sock
52+
serviceAccountName: {{.SpecTemplateSpecServiceAccountName}}
53+
restartPolicy: Never
54+
tolerations:
55+
- effect: NoSchedule
56+
key: node.kubernetes.io/ci
57+
operator: Exists
58+
- effect: PreferNoSchedule
59+
key: node.kubernetes.io/ci
60+
operator: Exists
61+
volumes:
62+
- configMap:
63+
defaultMode: 420
64+
items:
65+
- key: data
66+
path: config.json
67+
name: {{.ConfigMapName}}
68+
name: config-data
69+
- hostPath:
70+
path: /var/run/docker.sock
71+
type: ""
72+
name: docker-sock

pkg/config/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
type Config struct {
4+
S2IRunJobTemplate []byte // the template file content
5+
}

pkg/controller/controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"github.com/kubesphere/s2ioperator/pkg/config"
2021
"sigs.k8s.io/controller-runtime/pkg/manager"
2122
)
2223

2324
// AddToManagerFuncs is a list of functions to add all Controllers to the Manager
24-
var AddToManagerFuncs []func(manager.Manager) error
25+
var AddToManagerFuncs []func(mgr manager.Manager, cfg *config.Config) error
2526

2627
// AddToManager adds all Controllers to the Manager
27-
func AddToManager(m manager.Manager) error {
28+
func AddToManager(m manager.Manager, cfg *config.Config) error {
2829
for _, f := range AddToManagerFuncs {
29-
if err := f(m); err != nil {
30+
if err := f(m, cfg); err != nil {
3031
return err
3132
}
3233
}

pkg/controller/s2ibuilder/s2ibuilder_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"reflect"
2222

23+
"github.com/kubesphere/s2ioperator/pkg/config"
2324
"github.com/kubesphere/s2ioperator/pkg/util/sliceutil"
2425
v1 "k8s.io/api/apps/v1"
2526

@@ -50,7 +51,7 @@ const s2iBuilderFinalizerName = "s2ibuilders.finalizers.kubesphere.io"
5051

5152
// Add creates a new S2iBuilder Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
5253
// and Start it when the Manager is Started.
53-
func Add(mgr manager.Manager) error {
54+
func Add(mgr manager.Manager, cfg *config.Config) error {
5455
return add(mgr, newReconciler(mgr))
5556
}
5657

pkg/controller/s2irun/ksbuilder_jobs.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ type JobTemplateData struct {
138138
SpecTemplateSpecServiceAccountName string
139139
ContainerS2IRunImage string
140140
SpecBackoffLimit int32
141-
SpecTTLSecondsAfterFinished int32
142141
ConfigMapName string
143142
}
144143

@@ -158,21 +157,16 @@ func (r *ReconcileS2iRun) getJobTemplateData(instance *devopsv1alpha1.S2iRun) (*
158157
SpecTemplateSpecServiceAccountName: RegularServiceAccount,
159158
ContainerS2IRunImage: imageName,
160159
SpecBackoffLimit: instance.Spec.BackoffLimit,
161-
SpecTTLSecondsAfterFinished: instance.Spec.SecondsAfterFinished,
162160
ConfigMapName: configMapName,
163161
}
164162
return data, nil
165163
}
166164

167-
func (r *ReconcileS2iRun) GenerateNewJob(instance *devopsv1alpha1.S2iRun, templatePath string) (*batchv1.Job, error) {
165+
func (r *ReconcileS2iRun) GenerateNewJob(instance *devopsv1alpha1.S2iRun, templateContent []byte) (*batchv1.Job, error) {
168166
templateData, err := r.getJobTemplateData(instance)
169167
if err != nil {
170168
return nil, err
171169
}
172-
templateContent, err := os.ReadFile(templatePath)
173-
if err != nil {
174-
return nil, err
175-
}
176170
tmpl, err := template.New("job").Parse(string(templateContent))
177171
if err != nil {
178172
return nil, err
@@ -185,6 +179,12 @@ func (r *ReconcileS2iRun) GenerateNewJob(instance *devopsv1alpha1.S2iRun, templa
185179
var job batchv1.Job
186180
decoder := yaml.NewYAMLOrJSONDecoder(&buf, 4096)
187181
err = decoder.Decode(&job)
182+
if err != nil {
183+
return nil, err
184+
}
185+
if instance.Spec.SecondsAfterFinished > 0 {
186+
job.Spec.TTLSecondsAfterFinished = &instance.Spec.SecondsAfterFinished
187+
}
188188
return &job, err
189189
}
190190

pkg/controller/s2irun/s2irun_controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"reflect"
2424
"time"
2525

26+
"github.com/kubesphere/s2ioperator/pkg/config"
2627
v12 "k8s.io/api/rbac/v1"
2728

2829
devopsv1alpha1 "github.com/kubesphere/s2ioperator/pkg/apis/devops/v1alpha1"
@@ -55,7 +56,6 @@ const (
5556
RegularRoleName = "s2i-regular-role"
5657
RegularRoleBinding = "s2i-regular-rolebinding"
5758
DefaultRevisionId = "master"
58-
JobTemplateYaml = "/etc/data/job-template.yaml"
5959
)
6060

6161
/**
@@ -65,13 +65,17 @@ const (
6565

6666
// Add creates a new S2iRun Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
6767
// and Start it when the Manager is Started.
68-
func Add(mgr manager.Manager) error {
69-
return add(mgr, newReconciler(mgr))
68+
func Add(mgr manager.Manager, cfg *config.Config) error {
69+
return add(mgr, newReconciler(mgr, cfg))
7070
}
7171

7272
// newReconciler returns a new reconcile.Reconciler
73-
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
74-
return &ReconcileS2iRun{Client: mgr.GetClient(), scheme: mgr.GetScheme()}
73+
func newReconciler(mgr manager.Manager, cfg *config.Config) reconcile.Reconciler {
74+
return &ReconcileS2iRun{
75+
Client: mgr.GetClient(),
76+
scheme: mgr.GetScheme(),
77+
cfg: cfg,
78+
}
7579
}
7680

7781
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -112,6 +116,7 @@ var _ reconcile.Reconciler = &ReconcileS2iRun{}
112116
type ReconcileS2iRun struct {
113117
client.Client
114118
scheme *runtime.Scheme
119+
cfg *config.Config
115120
}
116121

117122
// Reconcile reads that state of the cluster for a S2iRun object and makes changes based on the state read
@@ -258,7 +263,7 @@ func (r *ReconcileS2iRun) Reconcile(ctx context.Context, request reconcile.Reque
258263
}
259264

260265
//job set up
261-
job, err := r.GenerateNewJob(instance, JobTemplateYaml)
266+
job, err := r.GenerateNewJob(instance, r.cfg.S2IRunJobTemplate)
262267
if err != nil {
263268
log.Error(err, "Failed to initialize a job")
264269
return reconcile.Result{}, err

0 commit comments

Comments
 (0)