Skip to content

Commit

Permalink
Merge pull request #529 from LiZhenCheng9527/rollout-controller-render
Browse files Browse the repository at this point in the history
complete rollout policy controller render
  • Loading branch information
kurator-bot authored Jan 10, 2024
2 parents 09a35eb + 6ad5854 commit e544361
Show file tree
Hide file tree
Showing 8 changed files with 1,194 additions and 1 deletion.
1 change: 1 addition & 0 deletions manifests/charts/fleet-manager/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ rules:
- create
- delete
- list
- update
- apiGroups:
- "source.toolkit.fluxcd.io"
resources:
Expand Down
16 changes: 15 additions & 1 deletion pkg/fleet-manager/application/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ import (

// syncPolicyResource synchronizes the sync policy resources for a given application.
func (a *ApplicationManager) syncPolicyResource(ctx context.Context, app *applicationapi.Application, fleet *fleetapi.Fleet, syncPolicy *applicationapi.ApplicationSyncPolicy, policyName string) (ctrl.Result, error) {
policyKind := getSyncPolicyKind(syncPolicy)
log := ctrl.LoggerFrom(ctx)

policyKind := getSyncPolicyKind(syncPolicy)
destination := getPolicyDestination(app, syncPolicy)

// fetch fleet cluster list that recorded in fleet and matches the destination's cluster selector
Expand All @@ -60,6 +61,19 @@ func (a *ApplicationManager) syncPolicyResource(ctx context.Context, app *applic
}
}

if syncPolicy.Rollout != nil {
// after finish application install, start handling rollout policy
rolloutClusters, err := a.fetchRolloutClusters(ctx, app, a.Client, fleet, syncPolicy)
if err != nil {
log.Error(err, "failed to fetch destination clusters for rollout")
return ctrl.Result{}, err
}

if result, err := a.syncRolloutPolicyForCluster(ctx, syncPolicy.Rollout, rolloutClusters, policyName); err != nil {
return result, errors.Wrapf(err, "failed to syncRolloutPolicy")
}
}

return ctrl.Result{}, nil
}

Expand Down
151 changes: 151 additions & 0 deletions pkg/fleet-manager/application/manifests/rollout_testloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright Kurator Authors.
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 render

import (
"bytes"
"text/template"

"k8s.io/apimachinery/pkg/types"
)

type TestloaderConfig struct {
Name string
Namespace string
LabelKey string
LabelValue string
AnnotationKey string
AnnotationValue string
}

const testloaderTemplateName = "testloader"

// RenderTestloaderConfig takes generates YAML byte array configuration representing the testloader configuration.
func RenderTestloaderConfig(constTemplateName string, namespacedName types.NamespacedName, annotationKey, annotationsValue string) ([]byte, error) {
cfg := TestloaderConfig{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
LabelKey: "app",
LabelValue: namespacedName.Name,
AnnotationKey: annotationKey,
AnnotationValue: annotationsValue,
}

return renderTestloaderTemplateConfig(constTemplateName, cfg)
}

// renderTestloaderTemplateConfig reads, parses, and renders a template file using the provided configuration data.
func renderTestloaderTemplateConfig(constTemplateName string, cfg TestloaderConfig) ([]byte, error) {
tql, err := template.New(testloaderTemplateName).Funcs(funMap()).Parse(constTemplateName)
if err != nil {
return nil, err
}

var b bytes.Buffer
if err := tql.Execute(&b, cfg); err != nil {
return nil, err
}

return b.Bytes(), nil
}

func funMap() template.FuncMap {
return nil
}

const TestlaoderDeployment = `apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.Name}}
namespace: {{.Namespace}}
labels:
{{.LabelKey}}: {{.LabelValue}}
annotations:
{{.AnnotationKey}}: {{.AnnotationValue}}
spec:
selector:
matchLabels:
{{.LabelKey}}: {{.LabelValue}}
template:
metadata:
labels:
{{.LabelKey}}: {{.LabelValue}}
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
openservicemesh.io/inbound-port-exclusion-list: "80, 8080"
spec:
containers:
- name: loadtester
image: ghcr.io/fluxcd/flagger-loadtester:0.29.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
command:
- ./loadtester
- -port=8080
- -log-level=info
- -timeout=1h
livenessProbe:
exec:
command:
- wget
- --quiet
- --tries=1
- --timeout=4
- --spider
- http://localhost:8080/healthz
timeoutSeconds: 5
readinessProbe:
exec:
command:
- wget
- --quiet
- --tries=1
- --timeout=4
- --spider
- http://localhost:8080/healthz
timeoutSeconds: 5
resources:
limits:
memory: "512Mi"
cpu: "1000m"
requests:
memory: "32Mi"
cpu: "10m"
securityContext:
readOnlyRootFilesystem: true
runAsUser: 10001
`

const TestlaoderService = `apiVersion: v1
kind: Service
metadata:
name: {{.Name}}
namespace: {{.Namespace}}
labels:
{{.LabelKey}}: {{.LabelValue}}
annotations:
{{.AnnotationKey}}: {{.AnnotationValue}}
spec:
type: ClusterIP
selector:
{{.LabelKey}}: {{.LabelValue}}
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
`
61 changes: 61 additions & 0 deletions pkg/fleet-manager/application/manifests/rollout_testloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright Kurator Authors.
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 render

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"
)

func TestRenderTestloaderConfig(t *testing.T) {
expectedTestDataFile := "testdata/"
namespacedName := types.NamespacedName{
Namespace: "test",
Name: "testloader",
}
annotationKey := "kurator.dev/rollout"
annotationValue := "policy"
cases := []struct {
name string
constTemplateName string
expectFileName string
}{
{
name: "testloader deploy template",
constTemplateName: TestlaoderDeployment,
expectFileName: "testloader-deploy.yaml",
},
{
name: "testloader svc template",
constTemplateName: TestlaoderService,
expectFileName: "testloader-svc.yaml",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := RenderTestloaderConfig(tc.constTemplateName, namespacedName, annotationKey, annotationValue)
if err != nil {
assert.Error(t, err)
} else {
expected, err := os.ReadFile(expectedTestDataFile + tc.expectFileName)
assert.NoError(t, err)
assert.Equal(t, result, expected)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: testloader
namespace: test
labels:
app: testloader
annotations:
kurator.dev/rollout: policy
spec:
selector:
matchLabels:
app: testloader
template:
metadata:
labels:
app: testloader
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
openservicemesh.io/inbound-port-exclusion-list: "80, 8080"
spec:
containers:
- name: loadtester
image: ghcr.io/fluxcd/flagger-loadtester:0.29.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
command:
- ./loadtester
- -port=8080
- -log-level=info
- -timeout=1h
livenessProbe:
exec:
command:
- wget
- --quiet
- --tries=1
- --timeout=4
- --spider
- http://localhost:8080/healthz
timeoutSeconds: 5
readinessProbe:
exec:
command:
- wget
- --quiet
- --tries=1
- --timeout=4
- --spider
- http://localhost:8080/healthz
timeoutSeconds: 5
resources:
limits:
memory: "512Mi"
cpu: "1000m"
requests:
memory: "32Mi"
cpu: "10m"
securityContext:
readOnlyRootFilesystem: true
runAsUser: 10001
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Service
metadata:
name: testloader
namespace: test
labels:
app: testloader
annotations:
kurator.dev/rollout: policy
spec:
type: ClusterIP
selector:
app: testloader
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
Loading

0 comments on commit e544361

Please sign in to comment.