Skip to content

Commit 5f22f06

Browse files
authored
add istio injection flag (#2248)
1 parent fce10c2 commit 5f22f06

File tree

5 files changed

+117
-8
lines changed

5 files changed

+117
-8
lines changed

internal/cmd/alpha/app/push.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ package app
33
import (
44
"github.com/kyma-project/cli.v3/internal/clierror"
55
"github.com/kyma-project/cli.v3/internal/cmdcommon"
6+
"github.com/kyma-project/cli.v3/internal/cmdcommon/types"
67
"github.com/kyma-project/cli.v3/internal/kube/resources"
78
"github.com/spf13/cobra"
89
)
910

1011
type appPushConfig struct {
1112
*cmdcommon.KymaConfig
1213

13-
name string
14-
namespace string
15-
image string
14+
name string
15+
namespace string
16+
image string
17+
istioInject types.NullableBool
1618
// containerPort int
1719
}
1820

@@ -33,6 +35,7 @@ func NewAppPushCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
3335
cmd.Flags().StringVar(&config.name, "name", "", "Name of the app")
3436
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace where app should be deployed")
3537
cmd.Flags().StringVar(&config.image, "image", "", "Name of the image to deploy")
38+
cmd.Flags().Var(&config.istioInject, "istio-inject", "Enable Istio for the app")
3639
// cmd.Flags().IntVar(&config.containerPort, "containerPort", 80, "")
3740
_ = cmd.MarkFlagRequired("name")
3841
_ = cmd.MarkFlagRequired("image")
@@ -45,7 +48,7 @@ func runAppPush(cfg *appPushConfig) clierror.Error {
4548
if clierr != nil {
4649
return clierr
4750
}
48-
err := resources.CreateDeployment(cfg.Ctx, client, cfg.name, cfg.namespace, cfg.image)
51+
err := resources.CreateDeployment(cfg.Ctx, client, cfg.name, cfg.namespace, cfg.image, cfg.istioInject)
4952
if err != nil {
5053
return clierror.Wrap(err, clierror.New("failed to create deployment"))
5154
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package types
2+
3+
import "strconv"
4+
5+
type NullableBool struct {
6+
Value *bool
7+
}
8+
9+
func (n *NullableBool) String() string {
10+
if n.Value == nil {
11+
return ""
12+
}
13+
return strconv.FormatBool(*n.Value)
14+
}
15+
16+
func (n *NullableBool) Set(value string) error {
17+
if value == "" {
18+
return nil
19+
}
20+
b, err := strconv.ParseBool(value)
21+
if err != nil {
22+
return err
23+
}
24+
n.Value = &b
25+
return nil
26+
}
27+
28+
func (n *NullableBool) Type() string {
29+
return "bool"
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package types_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/kyma-project/cli.v3/internal/cmdcommon/types"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
var (
11+
trueConst = true
12+
falseConst = false
13+
)
14+
15+
func TestNullableBool_Set(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
value string
19+
expected *bool
20+
expectedError bool
21+
}{
22+
{
23+
name: "empty",
24+
value: "",
25+
expected: nil,
26+
expectedError: false,
27+
},
28+
{
29+
name: "true",
30+
value: "true",
31+
expected: &trueConst,
32+
expectedError: false,
33+
},
34+
{
35+
name: "false",
36+
value: "false",
37+
expected: &falseConst,
38+
expectedError: false,
39+
},
40+
{
41+
name: "incorrect",
42+
value: "incorrect",
43+
expected: nil,
44+
expectedError: true,
45+
},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
nb := types.NullableBool{}
51+
err := nb.Set(tt.value)
52+
if tt.expectedError {
53+
require.Error(t, err)
54+
return
55+
}
56+
require.NoError(t, err)
57+
require.Equal(t, tt.expected, nb.Value)
58+
})
59+
}
60+
61+
}

internal/kube/resources/resources.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"context"
55

6+
"github.com/kyma-project/cli.v3/internal/cmdcommon/types"
67
"github.com/kyma-project/cli.v3/internal/kube"
78
appsv1 "k8s.io/api/apps/v1"
89
v1 "k8s.io/api/core/v1"
@@ -75,7 +76,7 @@ func CreateClusterRoleBinding(ctx context.Context, client kube.Client, name, nam
7576
return nil
7677
}
7778

78-
func CreateDeployment(ctx context.Context, client kube.Client, name, namespace, image string) error {
79+
func CreateDeployment(ctx context.Context, client kube.Client, name, namespace, image string, injectIstio types.NullableBool) error {
7980
deployment := &appsv1.Deployment{
8081
ObjectMeta: metav1.ObjectMeta{
8182
Name: name,
@@ -94,8 +95,7 @@ func CreateDeployment(ctx context.Context, client kube.Client, name, namespace,
9495
ObjectMeta: metav1.ObjectMeta{
9596
Name: name,
9697
Labels: map[string]string{
97-
"app": name,
98-
"sidecar.istio.io/inject": "false",
98+
"app": name,
9999
},
100100
},
101101
Spec: v1.PodSpec{
@@ -119,6 +119,9 @@ func CreateDeployment(ctx context.Context, client kube.Client, name, namespace,
119119
},
120120
},
121121
}
122+
if injectIstio.Value != nil {
123+
deployment.Spec.Template.ObjectMeta.Labels["sidecar.istio.io/inject"] = injectIstio.String()
124+
}
122125

123126
_, err := client.Static().AppsV1().Deployments(namespace).Create(ctx, deployment, metav1.CreateOptions{})
124127
return err

internal/kube/resources/resources_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/kyma-project/cli.v3/internal/cmdcommon/types"
78
kube_fake "github.com/kyma-project/cli.v3/internal/kube/fake"
89
"github.com/stretchr/testify/require"
910
appsv1 "k8s.io/api/apps/v1"
@@ -90,11 +91,13 @@ func Test_CreateClusterRoleBinding(t *testing.T) {
9091

9192
func Test_CreateDeployment(t *testing.T) {
9293
t.Parallel()
94+
trueValue := true
9395
tests := []struct {
9496
name string
9597
deploymentName string
9698
namespace string
9799
image string
100+
istioInject *bool
98101
wantErr bool
99102
}{
100103
{
@@ -104,6 +107,14 @@ func Test_CreateDeployment(t *testing.T) {
104107
image: "nginx",
105108
wantErr: false,
106109
},
110+
{
111+
name: "create deployment with istio label",
112+
deploymentName: "deployment",
113+
namespace: "default",
114+
image: "nginx",
115+
istioInject: &trueValue,
116+
wantErr: false,
117+
},
107118
{
108119
name: "do not allow creating existing deployment",
109120
deploymentName: "existing",
@@ -118,6 +129,7 @@ func Test_CreateDeployment(t *testing.T) {
118129
deploymentName := tt.deploymentName
119130
namespace := tt.namespace
120131
image := tt.image
132+
istioInject := tt.istioInject
121133
wantErr := tt.wantErr
122134

123135
t.Run(tt.name, func(t *testing.T) {
@@ -134,7 +146,7 @@ func Test_CreateDeployment(t *testing.T) {
134146
TestKubernetesInterface: staticClient,
135147
}
136148

137-
err := CreateDeployment(ctx, kubeClient, deploymentName, namespace, image)
149+
err := CreateDeployment(ctx, kubeClient, deploymentName, namespace, image, types.NullableBool{Value: istioInject})
138150
if wantErr {
139151
require.Error(t, err)
140152
} else {

0 commit comments

Comments
 (0)