@@ -28,81 +28,123 @@ func MakeUpdateHandler(functionNamespace string, clientset *kubernetes.Clientset
28
28
return
29
29
}
30
30
31
- getOpts := metav1. GetOptions {}
32
-
33
- deployment , findDeployErr := clientset . ExtensionsV1beta1 ().
34
- Deployments ( functionNamespace ).
35
- Get ( request . Service , getOpts )
31
+ annotations := buildAnnotations ( request )
32
+ if err , status := updateDeploymentSpec ( functionNamespace , clientset , request , annotations ); err != nil {
33
+ w . WriteHeader ( status )
34
+ w . Write ([] byte ( err . Error ()))
35
+ }
36
36
37
- if findDeployErr != nil {
38
- w .WriteHeader (http .StatusNotFound )
39
- w .Write ([]byte (findDeployErr .Error ()))
40
- return
37
+ if err , status := updateService (functionNamespace , clientset , request , annotations ); err != nil {
38
+ w .WriteHeader (status )
39
+ w .Write ([]byte (err .Error ()))
41
40
}
41
+ }
42
+ }
42
43
43
- if len (deployment .Spec .Template .Spec .Containers ) > 0 {
44
- deployment .Spec .Template .Spec .Containers [0 ].Image = request .Image
44
+ func updateDeploymentSpec (
45
+ functionNamespace string ,
46
+ clientset * kubernetes.Clientset ,
47
+ request requests.CreateFunctionRequest ,
48
+ annotations map [string ]string ) (err error , httpStatus int ) {
49
+ getOpts := metav1.GetOptions {}
45
50
46
- // Disabling update support to prevent unexpected mutations of deployed functions,
47
- // since imagePullPolicy is now configurable. This could be reconsidered later depending
48
- // on desired behavior, but will need to be updated to take config.
49
- //deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = v1.PullAlways
51
+ deployment , findDeployErr := clientset .ExtensionsV1beta1 ().
52
+ Deployments (functionNamespace ).
53
+ Get (request .Service , getOpts )
50
54
51
- deployment .Spec .Template .Spec .Containers [0 ].Env = buildEnvVars (& request )
55
+ if findDeployErr != nil {
56
+ return findDeployErr , http .StatusNotFound
57
+ }
52
58
53
- configureReadOnlyRootFilesystem (request , deployment )
59
+ if len (deployment .Spec .Template .Spec .Containers ) > 0 {
60
+ deployment .Spec .Template .Spec .Containers [0 ].Image = request .Image
54
61
55
- deployment .Spec .Template .Spec .NodeSelector = createSelector (request .Constraints )
62
+ // Disabling update support to prevent unexpected mutations of deployed functions,
63
+ // since imagePullPolicy is now configurable. This could be reconsidered later depending
64
+ // on desired behavior, but will need to be updated to take config.
65
+ //deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = v1.PullAlways
56
66
57
- labels := map [string ]string {
58
- "faas_function" : request .Service ,
59
- "uid" : fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
60
- }
67
+ deployment .Spec .Template .Spec .Containers [0 ].Env = buildEnvVars (& request )
61
68
62
- if request .Labels != nil {
63
- if min := getMinReplicaCount (* request .Labels ); min != nil {
64
- deployment .Spec .Replicas = min
65
- }
69
+ configureReadOnlyRootFilesystem (request , deployment )
66
70
67
- for k , v := range * request .Labels {
68
- labels [k ] = v
69
- }
70
- }
71
+ deployment .Spec .Template .Spec .NodeSelector = createSelector (request .Constraints )
71
72
72
- deployment .Labels = labels
73
- deployment .Spec .Template .ObjectMeta .Labels = labels
73
+ labels := map [string ]string {
74
+ "faas_function" : request .Service ,
75
+ "uid" : fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
76
+ }
74
77
75
- resources , resourceErr := createResources (request )
76
- if resourceErr != nil {
77
- w .WriteHeader (http .StatusBadRequest )
78
- w .Write ([]byte (resourceErr .Error ()))
79
- return
78
+ if request .Labels != nil {
79
+ if min := getMinReplicaCount (* request .Labels ); min != nil {
80
+ deployment .Spec .Replicas = min
80
81
}
81
82
82
- deployment .Spec .Template .Spec .Containers [0 ].Resources = * resources
83
-
84
- existingSecrets , err := getSecrets (clientset , functionNamespace , request .Secrets )
85
- if err != nil {
86
- w .WriteHeader (http .StatusBadRequest )
87
- w .Write ([]byte (err .Error ()))
88
- return
83
+ for k , v := range * request .Labels {
84
+ labels [k ] = v
89
85
}
86
+ }
90
87
91
- err = UpdateSecrets (request , deployment , existingSecrets )
92
- if err != nil {
93
- log .Println (err )
94
- w .WriteHeader (http .StatusBadRequest )
95
- w .Write ([]byte (err .Error ()))
96
- return
97
- }
88
+ deployment .Labels = labels
89
+ deployment .Spec .Template .ObjectMeta .Labels = labels
90
+
91
+ deployment .Annotations = annotations
92
+ deployment .Spec .Template .Annotations = annotations
93
+ deployment .Spec .Template .ObjectMeta .Annotations = annotations
94
+
95
+ resources , resourceErr := createResources (request )
96
+ if resourceErr != nil {
97
+ return resourceErr , http .StatusBadRequest
98
98
}
99
99
100
- if _ , updateErr := clientset .ExtensionsV1beta1 ().
101
- Deployments (functionNamespace ).
102
- Update (deployment ); updateErr != nil {
100
+ deployment .Spec .Template .Spec .Containers [0 ].Resources = * resources
103
101
104
- w .WriteHeader (http .StatusInternalServerError )
105
- w .Write ([]byte (updateErr .Error ()))
102
+ existingSecrets , err := getSecrets (clientset , functionNamespace , request .Secrets )
103
+ if err != nil {
104
+ return err , http .StatusBadRequest
106
105
}
106
+
107
+ err = UpdateSecrets (request , deployment , existingSecrets )
108
+ if err != nil {
109
+ log .Println (err )
110
+ return err , http .StatusBadRequest
111
+ }
112
+ }
113
+
114
+ if _ , updateErr := clientset .ExtensionsV1beta1 ().
115
+ Deployments (functionNamespace ).
116
+ Update (deployment ); updateErr != nil {
117
+
118
+ return updateErr , http .StatusInternalServerError
119
+ }
120
+
121
+ return nil , http .StatusOK
122
+ }
123
+
124
+ func updateService (
125
+ functionNamespace string ,
126
+ clientset * kubernetes.Clientset ,
127
+ request requests.CreateFunctionRequest ,
128
+ annotations map [string ]string ) (err error , httpStatus int ) {
129
+
130
+ getOpts := metav1.GetOptions {}
131
+
132
+ service , findServiceErr := clientset .CoreV1 ().
133
+ Services (functionNamespace ).
134
+ Get (request .Service , getOpts )
135
+
136
+ if findServiceErr != nil {
137
+ return findServiceErr , http .StatusNotFound
138
+ }
139
+
140
+ service .Annotations = annotations
141
+
142
+ if _ , updateErr := clientset .CoreV1 ().
143
+ Services (functionNamespace ).
144
+ Update (service ); updateErr != nil {
145
+
146
+ return updateErr , http .StatusInternalServerError
107
147
}
148
+
149
+ return nil , http .StatusOK
108
150
}
0 commit comments