Skip to content

Commit 8e599fd

Browse files
support app-proxy install (#182)
* support app-proxy install
1 parent 98f5919 commit 8e599fd

File tree

9 files changed

+346
-4
lines changed

9 files changed

+346
-4
lines changed

venona/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.21
1+
1.4.22

venonactl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.21
1+
1.4.22

venonactl/cmd/install-app-proxy.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cmd
2+
3+
/*
4+
Copyright 2019 The Codefresh Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
import (
20+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
21+
"github.com/codefresh-io/venona/venonactl/pkg/store"
22+
"github.com/spf13/cobra"
23+
"github.com/spf13/viper"
24+
)
25+
26+
var installAppProxyCmdOptions struct {
27+
kube struct {
28+
namespace string
29+
context string
30+
}
31+
}
32+
33+
var installAppProxyCmd = &cobra.Command{
34+
Use: "app-proxy",
35+
Short: "Install App proxy ",
36+
Run: func(cmd *cobra.Command, args []string) {
37+
s := store.GetStore()
38+
lgr := createLogger("Install-agent", verbose, logFormatter)
39+
buildBasicStore(lgr)
40+
builder := plugins.NewBuilder(lgr)
41+
if cfAPIHost == "" {
42+
cfAPIHost = "https://g.codefresh.io"
43+
}
44+
builderInstallOpt := &plugins.InstallOptions{
45+
CodefreshHost: cfAPIHost,
46+
}
47+
48+
extendStoreWithKubeClient(lgr)
49+
fillCodefreshAPI(lgr)
50+
fillKubernetesAPI(lgr, installAppProxyCmdOptions.kube.context, installAppProxyCmdOptions.kube.namespace, false)
51+
s.AgentAPI = &store.AgentAPI{
52+
Token: "",
53+
Id: "",
54+
}
55+
builderInstallOpt.ClusterName = s.KubernetesAPI.ContextName
56+
builderInstallOpt.KubeBuilder = getKubeClientBuilder(builderInstallOpt.ClusterName, s.KubernetesAPI.Namespace, s.KubernetesAPI.ConfigPath, s.KubernetesAPI.InCluster)
57+
builderInstallOpt.ClusterNamespace = s.KubernetesAPI.Namespace
58+
builder.Add(plugins.AppProxyPluginType)
59+
60+
values := s.BuildValues()
61+
var err error
62+
spn := createSpinner("Installing app proxy (might take a few minutes)", "")
63+
spn.Start()
64+
defer spn.Stop()
65+
for _, p := range builder.Get() {
66+
values, err = p.Install(builderInstallOpt, values)
67+
if err != nil {
68+
dieOnError(err)
69+
}
70+
}
71+
lgr.Info("App proxy installation completed Successfully")
72+
73+
},
74+
}
75+
76+
func init() {
77+
viper.BindEnv("kube-namespace", "KUBE_NAMESPACE")
78+
viper.BindEnv("kube-context", "KUBE_CONTEXT")
79+
installCommand.AddCommand(installAppProxyCmd)
80+
installAppProxyCmd.Flags().StringVar(&installAppProxyCmdOptions.kube.namespace, "kube-namespace", viper.GetString("kube-namespace"), "Name of the namespace on which venona should be installed [$KUBE_NAMESPACE]")
81+
installAppProxyCmd.Flags().StringVar(&installAppProxyCmdOptions.kube.context, "kube-context-name", viper.GetString("kube-context"), "Name of the kubernetes context on which venona should be installed (default is current-context) [$KUBE_CONTEXT]")
82+
}

venonactl/pkg/plugins/app-proxy.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package plugins
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"github.com/codefresh-io/venona/venonactl/pkg/logger"
10+
templates "github.com/codefresh-io/venona/venonactl/pkg/templates/kubernetes"
11+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
type appProxyPlugin struct {
15+
logger logger.Logger
16+
}
17+
18+
const (
19+
appProxyFilesPattern = ".*.app-proxy.yaml"
20+
)
21+
22+
func (u *appProxyPlugin) Install(opt *InstallOptions, v Values) (Values, error) {
23+
24+
cs, err := opt.KubeBuilder.BuildClient()
25+
if err != nil {
26+
u.logger.Error(fmt.Sprintf("Cannot create kubernetes clientset: %v ", err))
27+
return nil, err
28+
}
29+
err = opt.KubeBuilder.EnsureNamespaceExists(cs)
30+
if err != nil {
31+
u.logger.Error(fmt.Sprintf("Cannot ensure namespace exists: %v", err))
32+
return nil, err
33+
}
34+
err = install(&installOptions{
35+
logger: u.logger,
36+
templates: templates.TemplatesMap(),
37+
templateValues: v,
38+
kubeClientSet: cs,
39+
namespace: opt.ClusterNamespace,
40+
matchPattern: appProxyFilesPattern,
41+
dryRun: opt.DryRun,
42+
operatorType: AppProxyPluginType,
43+
})
44+
if err != nil {
45+
u.logger.Error(fmt.Sprintf("AppProxy installation failed: %v", err))
46+
return nil, err
47+
}
48+
// locating the serivce and get it's internal api
49+
50+
var ingressIP string
51+
ticker := time.NewTicker(5 * time.Second)
52+
Loop:
53+
for {
54+
select {
55+
case <-ticker.C:
56+
u.logger.Debug("Checking for app-proxy-service")
57+
service, err := cs.CoreV1().Services(opt.ClusterNamespace).Get("app-proxy-service", v1.GetOptions{})
58+
if err == nil {
59+
ips := service.Status.LoadBalancer.Ingress
60+
if len(ips) > 0 {
61+
ingressIP = ips[0].IP
62+
break Loop
63+
}
64+
}
65+
case <-time.After(600 * time.Second):
66+
u.logger.Error("Failed to get app-proxy-service internal ip")
67+
return v, fmt.Errorf("Failed to get app-proxy-service internal ip")
68+
}
69+
}
70+
// update IPC
71+
file := os.NewFile(3, "pipe")
72+
data := map[string]interface{}{
73+
"ingressIP": ingressIP,
74+
}
75+
var jsonData []byte
76+
jsonData, err = json.Marshal(data)
77+
n, err := file.Write(jsonData)
78+
if err != nil {
79+
u.logger.Error("Failed to write to stream", err)
80+
return v, fmt.Errorf("Failed to write to stream")
81+
}
82+
u.logger.Info(fmt.Sprintf("%s bytes were written to stream", n))
83+
fmt.Sprintf(" ip : %s", ingressIP)
84+
return v, err
85+
86+
}
87+
88+
func (u *appProxyPlugin) Status(statusOpt *StatusOptions, v Values) ([][]string, error) {
89+
return [][]string{}, nil
90+
}
91+
92+
func (u *appProxyPlugin) Delete(deleteOpt *DeleteOptions, v Values) error {
93+
cs, err := deleteOpt.KubeBuilder.BuildClient()
94+
if err != nil {
95+
u.logger.Error(fmt.Sprintf("Cannot create kubernetes clientset: %v ", err))
96+
return err
97+
}
98+
opt := &deleteOptions{
99+
templates: templates.TemplatesMap(),
100+
templateValues: v,
101+
kubeClientSet: cs,
102+
namespace: deleteOpt.ClusterNamespace,
103+
matchPattern: appProxyFilesPattern,
104+
operatorType: AppProxyPluginType,
105+
logger: u.logger,
106+
}
107+
return uninstall(opt)
108+
}
109+
110+
func (u *appProxyPlugin) Upgrade(opt *UpgradeOptions, v Values) (Values, error) {
111+
return nil, nil
112+
}
113+
func (u *appProxyPlugin) Migrate(*MigrateOptions, Values) error {
114+
return fmt.Errorf("not supported")
115+
}
116+
117+
func (u *appProxyPlugin) Test(opt TestOptions) error {
118+
return nil
119+
}
120+
121+
func (u *appProxyPlugin) Name() string {
122+
return AppProxyPluginType
123+
}

venonactl/pkg/plugins/plugin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
EnginePluginType = "engine"
2020
DefaultStorageClassNamePrefix = "dind-local-volumes-runner"
2121
RuntimeAttachType = "runtime-attach"
22+
AppProxyPluginType = "app-proxy"
2223
)
2324

2425
type (
@@ -219,6 +220,12 @@ func build(t string, logger logger.Logger) Plugin {
219220
}
220221
}
221222

223+
if t == AppProxyPluginType {
224+
return &appProxyPlugin{
225+
logger: logger.New("installer", AppProxyPluginType),
226+
}
227+
}
228+
222229
return nil
223230
}
224231

venonactl/pkg/store/store.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
ModeInCluster = "InCluster"
1212
ApplicationName = "runner"
1313
MonitorApplicationName = "monitor"
14+
AppProxy = "app-proxy"
1415
)
1516

1617
var (
@@ -126,7 +127,7 @@ func (s *Values) BuildValues() map[string]interface{} {
126127
"CreateRbac": true,
127128
"Storage": map[string]interface{}{
128129
"Backend": "local",
129-
"CreateStorageClass": true,
130+
"CreateStorageClass": true,
130131
"StorageClassName": fmt.Sprintf("dind-local-volumes-%s-%s", ApplicationName, s.KubernetesAPI.Namespace),
131132
"LocalVolumeParentDir": "/var/lib/codefresh/dind-volumes",
132133
"AvailabilityZone": "",
@@ -140,7 +141,7 @@ func (s *Values) BuildValues() map[string]interface{} {
140141
},
141142
},
142143
"Monitor": map[string]interface{}{
143-
"Enabled": true,
144+
"Enabled": true,
144145
"UseNamespaceWithRole": s.UseNamespaceWithRole,
145146
//TODO: need verify it on cluster level
146147
"RbacEnabled": true,
@@ -151,5 +152,12 @@ func (s *Values) BuildValues() map[string]interface{} {
151152
"Tag": "stable",
152153
},
153154
},
155+
"AppProxy": map[string]interface{}{
156+
"AppName": AppProxy,
157+
"Image": map[string]string{
158+
"Name": "codefresh/cf-app-proxy",
159+
"Tag": "latest",
160+
},
161+
},
154162
}
155163
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
labels:
5+
app: {{ .AppProxy.AppName }}
6+
version: {{ .Version }}
7+
name: {{ .AppProxy.AppName }}
8+
namespace: {{ .Namespace }}
9+
spec:
10+
selector:
11+
matchLabels:
12+
app: {{ .AppProxy.AppName }}
13+
version: {{ .Version }}
14+
replicas: 1
15+
revisionHistoryLimit: 5
16+
strategy:
17+
rollingUpdate:
18+
maxSurge: 50%
19+
maxUnavailable: 50%
20+
type: RollingUpdate
21+
template:
22+
metadata:
23+
labels:
24+
app: {{ .AppProxy.AppName }}
25+
version: {{ .Version }}
26+
spec:
27+
containers:
28+
- name: {{ .AppProxy.AppName }}
29+
image: {{ if ne .DockerRegistry ""}} {{- .DockerRegistry }}/{{ .AppProxy.Image.Name }}:{{ .AppProxy.Image.Tag }} {{- else }} {{- .AppProxy.Image.Name }}:{{ .AppProxy.Image.Tag }} {{- end}}
30+
imagePullPolicy: Always
31+
env:
32+
- name: PORT
33+
value: "3000"
34+
- name: CODEFRESH_HOST
35+
value: {{ .CodefreshHost }}
36+
ports:
37+
- containerPort: 3000
38+
protocol: TCP
39+
readinessProbe:
40+
httpGet:
41+
path: /health
42+
port: 3000
43+
periodSeconds: 5
44+
timeoutSeconds: 5
45+
successThreshold: 1
46+
failureThreshold: 5
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: app-proxy-service
5+
namespace: {{ .Namespace }}
6+
spec:
7+
selector:
8+
app: {{ .AppProxy.AppName }}
9+
ports:
10+
- protocol: TCP
11+
port: 80
12+
targetPort: 3000
13+
type: LoadBalancer

0 commit comments

Comments
 (0)