Skip to content

Commit b8a4207

Browse files
authored
Merge pull request #15 from snapp-incubator/create-svc
adding service creation for deployments
2 parents e34c8c2 + 9dd1af9 commit b8a4207

File tree

8 files changed

+104
-8
lines changed

8 files changed

+104
-8
lines changed

api/v1alpha1/basicauthenticator_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type BasicAuthenticatorSpec struct {
3535
// +kubebuilder:validation:Optional
3636
Selector metav1.LabelSelector `json:"selector,omitempty"`
3737

38+
// +kubebuilder:validation:Optional
39+
// +kubebuilder:default=ClusterIP
40+
ServiceType string `json:"serviceType"`
41+
3842
// +kubebuilder:validation:Required
3943
AppPort int `json:"appPort"`
4044

config/crd/bases/authenticator.snappcloud.io_basicauthenticators.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ spec:
100100
type: object
101101
type: object
102102
x-kubernetes-map-type: atomic
103+
serviceType:
104+
default: ClusterIP
105+
type: string
103106
type:
104107
description: Type is used to determine that nginx should be sidercar
105108
or deployment

config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,15 @@ rules:
6767
- patch
6868
- update
6969
- watch
70+
- apiGroups:
71+
- ""
72+
resources:
73+
- services
74+
verbs:
75+
- create
76+
- delete
77+
- get
78+
- list
79+
- patch
80+
- update
81+
- watch

internal/controller/basic_authenticator/basicauthenticator_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/snapp-incubator/simple-authenticator/internal/config"
2424
appv1 "k8s.io/api/apps/v1"
2525
corev1 "k8s.io/api/core/v1"
26+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/apimachinery/pkg/types"
2829
ctrl "sigs.k8s.io/controller-runtime"
@@ -41,6 +42,7 @@ type BasicAuthenticatorReconciler struct {
4142
configMapName string
4243
credentialName string
4344
basicAuthenticatorNamespace string
45+
deploymentLabel *v1.LabelSelector
4446
logger logr.Logger
4547
}
4648

@@ -72,6 +74,7 @@ func (r *BasicAuthenticatorReconciler) SetupWithManager(mgr ctrl.Manager) error
7274
Owns(&appv1.Deployment{}).
7375
Owns(&corev1.ConfigMap{}).
7476
Owns(&corev1.Secret{}).
77+
Owns(&corev1.Service{}).
7578
Watches(
7679
&source.Kind{Type: &appv1.Deployment{}},
7780
handler.EnqueueRequestsFromMapFunc(r.findExternallyManagedDeployments),

internal/controller/basic_authenticator/provision.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (r *BasicAuthenticatorReconciler) Provision(ctx context.Context, req ctrl.R
2222
r.ensureSecret,
2323
r.ensureConfigmap,
2424
r.ensureDeployment,
25+
r.ensureService,
2526
}
2627
for _, provisioner := range subProvisioner {
2728
result, err := provisioner(ctx, req)
@@ -177,7 +178,45 @@ func (r *BasicAuthenticatorReconciler) ensureDeployment(ctx context.Context, req
177178
return r.createDeploymentAuthenticator(ctx, req, basicAuthenticator, r.configMapName, r.credentialName)
178179
}
179180
}
181+
func (r *BasicAuthenticatorReconciler) ensureService(ctx context.Context, req ctrl.Request) (*ctrl.Result, error) {
182+
basicAuthenticator := &v1alpha1.BasicAuthenticator{}
183+
184+
if r, err := r.getLatestBasicAuthenticator(ctx, req, basicAuthenticator); subreconciler.ShouldHaltOrRequeue(r, err) {
185+
return r, err
186+
}
187+
if r.deploymentLabel == nil {
188+
return subreconciler.ContinueReconciling()
189+
}
190+
newService := createNginxService(ctx, basicAuthenticator, r.deploymentLabel)
191+
foundService := corev1.Service{}
192+
err := r.Get(ctx, types.NamespacedName{Name: newService.Name, Namespace: newService.Namespace}, &foundService)
193+
if errors.IsNotFound(err) {
194+
if err := ctrl.SetControllerReference(basicAuthenticator, newService, r.Scheme); err != nil {
195+
r.logger.Error(err, "failed to set service owner")
196+
return subreconciler.RequeueWithError(err)
197+
}
198+
err := r.Create(ctx, newService)
199+
if err != nil {
200+
r.logger.Error(err, "failed to create new service")
201+
return subreconciler.RequeueWithError(err)
202+
}
180203

204+
} else if err != nil {
205+
r.logger.Error(err, "failed to fetch service")
206+
return subreconciler.RequeueWithError(err)
207+
} else {
208+
if !reflect.DeepEqual(newService.Spec, foundService.Spec) {
209+
r.logger.Info("updating service")
210+
foundService.Spec = newService.Spec
211+
err := r.Update(ctx, &foundService)
212+
if err != nil {
213+
r.logger.Error(err, "failed to update service")
214+
return subreconciler.RequeueWithError(err)
215+
}
216+
}
217+
}
218+
return subreconciler.ContinueReconciling()
219+
}
181220
func (r *BasicAuthenticatorReconciler) createDeploymentAuthenticator(ctx context.Context, req ctrl.Request, basicAuthenticator *v1alpha1.BasicAuthenticator, authenticatorConfigName, secretName string) (*ctrl.Result, error) {
182221

183222
newDeployment := createNginxDeployment(basicAuthenticator, authenticatorConfigName, secretName, r.CustomConfig)
@@ -203,12 +242,10 @@ func (r *BasicAuthenticatorReconciler) createDeploymentAuthenticator(ctx context
203242
return subreconciler.RequeueWithError(err)
204243
}
205244
r.logger.Info("created deployment")
206-
245+
r.deploymentLabel = newDeployment.Spec.Selector
207246
} else if err != nil {
208-
if err != nil {
209-
r.logger.Error(err, "failed to fetch deployment")
210-
return subreconciler.RequeueWithError(err)
211-
}
247+
r.logger.Error(err, "failed to fetch deployment")
248+
return subreconciler.RequeueWithError(err)
212249
} else {
213250
//update deployment
214251
targetReplica := newDeployment.Spec.Replicas

internal/controller/basic_authenticator/nginx.go renamed to internal/controller/basic_authenticator/workload.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
corev1 "k8s.io/api/core/v1"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/labels"
16+
"k8s.io/apimachinery/pkg/util/intstr"
1617
"sigs.k8s.io/controller-runtime/pkg/client"
1718
"strings"
1819
)
@@ -90,7 +91,6 @@ func createNginxDeployment(basicAuthenticator *v1alpha1.BasicAuthenticator, conf
9091
},
9192
},
9293
}
93-
9494
return deploy
9595
}
9696

@@ -153,7 +153,29 @@ func createCredentials(basicAuthenticator *v1alpha1.BasicAuthenticator) (*corev1
153153
}
154154
return secret, nil
155155
}
156-
156+
func createNginxService(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, selector *metav1.LabelSelector) *corev1.Service {
157+
serviceName := fmt.Sprintf("%s-svc", basicAuthenticator.Name)
158+
serviceType := getServiceType(basicAuthenticator.Spec.ServiceType)
159+
targetPort := intstr.IntOrString{Type: intstr.Int, IntVal: int32(basicAuthenticator.Spec.AuthenticatorPort)}
160+
svc := corev1.Service{
161+
ObjectMeta: metav1.ObjectMeta{
162+
Name: serviceName,
163+
Namespace: basicAuthenticator.Namespace,
164+
},
165+
Spec: corev1.ServiceSpec{
166+
Selector: selector.MatchLabels,
167+
Type: serviceType,
168+
Ports: []corev1.ServicePort{
169+
{
170+
Port: int32(basicAuthenticator.Spec.AuthenticatorPort),
171+
TargetPort: targetPort,
172+
Name: "authenticator",
173+
},
174+
},
175+
},
176+
}
177+
return &svc
178+
}
157179
func injector(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, configMapName string, credentialName string, customConfig *config.CustomConfig, k8Client client.Client) ([]*appsv1.Deployment, error) {
158180
nginxImageAddress := getNginxContainerImage(customConfig)
159181
nginxContainerName := getNginxContainerName(customConfig)
@@ -236,3 +258,14 @@ func fillTemplate(template string, secretPath string, authenticator *v1alpha1.Ba
236258
result = strings.Replace(result, "APP_PORT", fmt.Sprintf("%d", authenticator.Spec.AppPort), 1)
237259
return result
238260
}
261+
262+
func getServiceType(serviceType string) corev1.ServiceType {
263+
switch serviceType {
264+
case "NodePort":
265+
return corev1.ServiceTypeNodePort
266+
case "LoadBalancer":
267+
return corev1.ServiceTypeLoadBalancer
268+
default:
269+
return corev1.ServiceTypeClusterIP
270+
}
271+
}

tests/e2e/deployment/00-install.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ spec:
1414
appPort: 8081
1515
appService: google.com
1616
adaptiveScale: false
17-
authenticatorPort: 8080
17+
authenticatorPort: 8082
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: basicauthenticator-sample-svc

0 commit comments

Comments
 (0)