-
Notifications
You must be signed in to change notification settings - Fork 37
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 #529 from LiZhenCheng9527/rollout-controller-render
complete rollout policy controller render
- Loading branch information
Showing
8 changed files
with
1,194 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -162,6 +162,7 @@ rules: | |
- create | ||
- delete | ||
- list | ||
- update | ||
- apiGroups: | ||
- "source.toolkit.fluxcd.io" | ||
resources: | ||
|
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
151 changes: 151 additions & 0 deletions
151
pkg/fleet-manager/application/manifests/rollout_testloader.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,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
61
pkg/fleet-manager/application/manifests/rollout_testloader_test.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,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) | ||
} | ||
}) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
pkg/fleet-manager/application/manifests/testdata/testloader-deploy.yaml
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,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 |
18 changes: 18 additions & 0 deletions
18
pkg/fleet-manager/application/manifests/testdata/testloader-svc.yaml
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,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 |
Oops, something went wrong.