Skip to content

Commit

Permalink
feat: change workload type to adapt to the new module (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed May 13, 2024
1 parent be37829 commit 6b08fab
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 80 deletions.
60 changes: 30 additions & 30 deletions pkg/apis/internal.kusion.io/v1/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,85 +35,85 @@ func TestContainerMarshalJSON(t *testing.T) {
Image: "nginx:v1",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{"service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
},
InitialDelaySeconds: 10,
},
},
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"kam.v1.workload.container.probe.Http","url":"http://localhost:80"},"initialDelaySeconds":10}}`,
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"service.container.probe.Http","url":"http://localhost:80"},"initialDelaySeconds":10}}`,
},
{
input: Container{
Image: "nginx:v1",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{"service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"cat", "/tmp/healthy"},
},
},
InitialDelaySeconds: 10,
},
},
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"kam.v1.workload.container.probe.Exec","command":["cat","/tmp/healthy"]},"initialDelaySeconds":10}}`,
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"service.container.probe.Exec","command":["cat","/tmp/healthy"]},"initialDelaySeconds":10}}`,
},
{
input: Container{
Image: "nginx:v1",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{Type: "kam.v1.workload.container.probe.Tcp"},
TypeWrapper: TypeWrapper{Type: "service.container.probe.Tcp"},
TCPSocketAction: &TCPSocketAction{
URL: "127.0.0.1:8080",
},
},
InitialDelaySeconds: 10,
},
},
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"kam.v1.workload.container.probe.Tcp","url":"127.0.0.1:8080"},"initialDelaySeconds":10}}`,
result: `{"image":"nginx:v1","readinessProbe":{"probeHandler":{"_type":"service.container.probe.Tcp","url":"127.0.0.1:8080"},"initialDelaySeconds":10}}`,
},
{
input: Container{
Image: "nginx:v1",
Lifecycle: &Lifecycle{
PostStart: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{"service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"/bin/sh", "-c", "nginx -s quit; while killall -0 nginx; do sleep 1; done"},
},
},
PreStop: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{"service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"},
},
},
},
},
result: `{"image":"nginx:v1","lifecycle":{"preStop":{"_type":"kam.v1.workload.container.probe.Exec","command":["/bin/sh","-c","echo Hello from the postStart handler \u003e /usr/share/message"]},"postStart":{"_type":"kam.v1.workload.container.probe.Exec","command":["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]}}}`,
result: `{"image":"nginx:v1","lifecycle":{"preStop":{"_type":"service.container.probe.Exec","command":["/bin/sh","-c","echo Hello from the postStart handler \u003e /usr/share/message"]},"postStart":{"_type":"service.container.probe.Exec","command":["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]}}}`,
},
{
input: Container{
Image: "nginx:v1",
Lifecycle: &Lifecycle{
PostStart: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{"service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
},
PreStop: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{"service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
},
},
},
result: `{"image":"nginx:v1","lifecycle":{"preStop":{"_type":"kam.v1.workload.container.probe.Http","url":"http://localhost:80"},"postStart":{"_type":"kam.v1.workload.container.probe.Http","url":"http://localhost:80"}}}`,
result: `{"image":"nginx:v1","lifecycle":{"preStop":{"_type":"service.container.probe.Http","url":"http://localhost:80"},"postStart":{"_type":"service.container.probe.Http","url":"http://localhost:80"}}}`,
},
}

Expand Down Expand Up @@ -174,7 +174,7 @@ workingDir: /tmp
WorkingDir: "/tmp",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{Type: "kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{Type: "service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
Expand All @@ -196,7 +196,7 @@ env:
workingDir: /tmp
readinessProbe:
probeHandler:
_type: kam.v1.workload.container.probe.Http
_type: service.container.probe.Http
url: http://localhost:80
initialDelaySeconds: 10
`,
Expand All @@ -215,7 +215,7 @@ readinessProbe:
WorkingDir: "/tmp",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{Type: "kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{Type: "service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"cat", "/tmp/healthy"},
},
Expand All @@ -237,7 +237,7 @@ env:
workingDir: /tmp
readinessProbe:
probeHandler:
_type: kam.v1.workload.container.probe.Exec
_type: service.container.probe.Exec
command:
- cat
- /tmp/healthy
Expand All @@ -258,7 +258,7 @@ readinessProbe:
WorkingDir: "/tmp",
ReadinessProbe: &Probe{
ProbeHandler: &ProbeHandler{
TypeWrapper: TypeWrapper{Type: "kam.v1.workload.container.probe.Tcp"},
TypeWrapper: TypeWrapper{Type: "service.container.probe.Tcp"},
TCPSocketAction: &TCPSocketAction{
URL: "127.0.0.1:8080",
},
Expand All @@ -280,7 +280,7 @@ env:
workingDir: /tmp
readinessProbe:
probeHandler:
_type: kam.v1.workload.container.probe.Tcp
_type: service.container.probe.Tcp
url: 127.0.0.1:8080
initialDelaySeconds: 10
`,
Expand All @@ -299,13 +299,13 @@ readinessProbe:
WorkingDir: "/tmp",
Lifecycle: &Lifecycle{
PostStart: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{"service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"/bin/sh", "-c", "nginx -s quit; while killall -0 nginx; do sleep 1; done"},
},
},
PreStop: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Exec"},
TypeWrapper: TypeWrapper{"service.container.probe.Exec"},
ExecAction: &ExecAction{
Command: []string{"/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"},
},
Expand All @@ -326,13 +326,13 @@ env:
workingDir: /tmp
lifecycle:
preStop:
_type: kam.v1.workload.container.probe.Exec
_type: service.container.probe.Exec
command:
- /bin/sh
- -c
- echo Hello from the postStart handler > /usr/share/message
postStart:
_type: kam.v1.workload.container.probe.Exec
_type: service.container.probe.Exec
command:
- /bin/sh
- -c
Expand All @@ -353,13 +353,13 @@ lifecycle:
WorkingDir: "/tmp",
Lifecycle: &Lifecycle{
PostStart: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{"service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
},
PreStop: &LifecycleHandler{
TypeWrapper: TypeWrapper{"kam.v1.workload.container.probe.Http"},
TypeWrapper: TypeWrapper{"service.container.probe.Http"},
HTTPGetAction: &HTTPGetAction{
URL: "http://localhost:80",
},
Expand All @@ -380,10 +380,10 @@ env:
workingDir: /tmp
lifecycle:
preStop:
_type: kam.v1.workload.container.probe.Http
_type: service.container.probe.Http
url: http://localhost:80
postStart:
_type: kam.v1.workload.container.probe.Http
_type: service.container.probe.Http
url: http://localhost:80
`,
},
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestWorkloadMarshalJSON(t *testing.T) {
Schedule: "* * * * *",
},
},
expected: `{"_type": "kam.v1.workload.Service", "replicas": 2, "labels": {"app": "my-service"}, "type": "Deployment"}`,
expected: `{"_type": "service.Service", "replicas": 2, "labels": {"app": "my-service"}, "type": "Deployment"}`,
expectedError: nil,
},
{
Expand All @@ -440,7 +440,7 @@ func TestWorkloadMarshalJSON(t *testing.T) {
Schedule: "* * * * *",
},
},
expected: `{"_type": "kam.v1.workload.Job", "schedule": "* * * * *"}`,
expected: `{"_type": "job.Job", "schedule": "* * * * *"}`,
expectedError: nil,
},
{
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestWorkloadMarshalYAML(t *testing.T) {
},
},
},
expected: `{"_type":"kam.v1.workload.Service", "replicas": 2, "labels": {"app": "my-service"}, "type": "Deployment"}`,
expected: `{"_type":"service.Service", "replicas": 2, "labels": {"app": "my-service"}, "type": "Deployment"}`,
expectedError: nil,
},
{
Expand All @@ -508,7 +508,7 @@ func TestWorkloadMarshalYAML(t *testing.T) {
Schedule: "* * * * *",
},
},
expected: `{"_type":"kam.v1.workload.Job", "schedule": "* * * * *"}`,
expected: `{"_type":"job.Job", "schedule": "* * * * *"}`,
expectedError: nil,
},
{
Expand Down
13 changes: 6 additions & 7 deletions pkg/apis/internal.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
)

const (
BuiltinModulePrefix = "kam."
ProbePrefix = "v1.workload.container.probe."
TypeHTTP = BuiltinModulePrefix + ProbePrefix + "Http"
TypeExec = BuiltinModulePrefix + ProbePrefix + "Exec"
TypeTCP = BuiltinModulePrefix + ProbePrefix + "Tcp"
ProbePrefix = "service.container.probe."
TypeHTTP = ProbePrefix + "Http"
TypeExec = ProbePrefix + "Exec"
TypeTCP = ProbePrefix + "Tcp"
)

// Container describes how the App's tasks are expected to be run.
Expand Down Expand Up @@ -217,8 +216,8 @@ type Job struct {
type Type string

const (
TypeJob = "kam.v1.workload.Job"
TypeService = "kam.v1.workload.Service"
TypeJob = "job.Job"
TypeService = "service.Service"
)

type Header struct {
Expand Down

0 comments on commit 6b08fab

Please sign in to comment.