Skip to content

Commit

Permalink
made nginx functions private
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamna committed Dec 2, 2023
1 parent 0245920 commit 5d406b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
27 changes: 14 additions & 13 deletions internal/controller/basic_authenticator/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"github.com/snapp-incubator/simple-authenticator/api/v1alpha1"
"github.com/snapp-incubator/simple-authenticator/internal/config"
"github.com/snapp-incubator/simple-authenticator/pkg/hash"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -36,15 +37,15 @@ const (
)

// TODO: come up with better name that "nginx"
func (r *BasicAuthenticatorReconciler) CreateNginxDeployment(basicAuthenticator *v1alpha1.BasicAuthenticator, configMapName string, credentialName string) *appsv1.Deployment {
func createNginxDeployment(basicAuthenticator *v1alpha1.BasicAuthenticator, configMapName string, credentialName string, customConfig *config.CustomConfig) *appsv1.Deployment {
nginxImageAddress := nginxDefaultImageAddress
if r.CustomConfig != nil && r.CustomConfig.WebserverConf.Image != "" {
nginxImageAddress = r.CustomConfig.WebserverConf.Image
if customConfig != nil && customConfig.WebserverConf.Image != "" {
nginxImageAddress = customConfig.WebserverConf.Image
}

nginxContainerName := nginxDefaultContainerName
if r.CustomConfig != nil && r.CustomConfig.WebserverConf.ContainerName != "" {
nginxContainerName = r.CustomConfig.WebserverConf.ContainerName
if customConfig != nil && customConfig.WebserverConf.ContainerName != "" {
nginxContainerName = customConfig.WebserverConf.ContainerName
}

deploymentName := GenerateRandomName(basicAuthenticator.Name, "deployment")
Expand Down Expand Up @@ -118,7 +119,7 @@ func (r *BasicAuthenticatorReconciler) CreateNginxDeployment(basicAuthenticator
return deploy
}

func (r *BasicAuthenticatorReconciler) CreateNginxConfigmap(basicAuthenticator *v1alpha1.BasicAuthenticator) *corev1.ConfigMap {
func createNginxConfigmap(basicAuthenticator *v1alpha1.BasicAuthenticator) *corev1.ConfigMap {
configmapName := GenerateRandomName(basicAuthenticator.Name, "configmap")
labels := map[string]string{
"app": basicAuthenticator.Name,
Expand All @@ -138,7 +139,7 @@ func (r *BasicAuthenticatorReconciler) CreateNginxConfigmap(basicAuthenticator *
return configMap
}

func (r *BasicAuthenticatorReconciler) CreateCredentials(basicAuthenticator *v1alpha1.BasicAuthenticator) *corev1.Secret {
func createCredentials(basicAuthenticator *v1alpha1.BasicAuthenticator) *corev1.Secret {
username, password := hash.GenerateRandomString(20), hash.GenerateRandomString(20)
htpasswdString := fmt.Sprintf("%s:%s", username, password)
secretName := GenerateRandomName(basicAuthenticator.Name, hash.GenerateRandomString(10))
Expand All @@ -154,19 +155,19 @@ func (r *BasicAuthenticatorReconciler) CreateCredentials(basicAuthenticator *v1a
return secret
}

func (r *BasicAuthenticatorReconciler) Injector(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, configMapName string, credentialName string) ([]*appsv1.Deployment, error) {
func injector(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, configMapName string, credentialName string, customConfig *config.CustomConfig, k8Client client.Client) ([]*appsv1.Deployment, error) {
nginxImageAddress := nginxDefaultImageAddress
if r.CustomConfig != nil && r.CustomConfig.WebserverConf.Image != "" {
nginxImageAddress = r.CustomConfig.WebserverConf.Image
if customConfig != nil && customConfig.WebserverConf.Image != "" {
nginxImageAddress = customConfig.WebserverConf.Image
}

nginxContainerName := nginxDefaultContainerName
if r.CustomConfig != nil && r.CustomConfig.WebserverConf.ContainerName != "" {
nginxContainerName = r.CustomConfig.WebserverConf.ContainerName
if customConfig != nil && customConfig.WebserverConf.ContainerName != "" {
nginxContainerName = customConfig.WebserverConf.ContainerName
}
authenticatorPort := int32(basicAuthenticator.Spec.AuthenticatorPort)
var deploymentList appsv1.DeploymentList
if err := r.Client.List(
if err := k8Client.List(
ctx,
&deploymentList,
client.MatchingLabelsSelector{Selector: labels.SelectorFromSet(basicAuthenticator.Spec.Selector.MatchLabels)},
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/basic_authenticator/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *BasicAuthenticatorReconciler) ensureSecret(ctx context.Context, req ctr
var credentialSecret corev1.Secret
if credentialName == "" {
//create secret
newSecret := r.CreateCredentials(basicAuthenticator)
newSecret := createCredentials(basicAuthenticator)
err := r.Get(ctx, types.NamespacedName{Name: newSecret.Name, Namespace: newSecret.Namespace}, &credentialSecret)
if errors.IsNotFound(err) {
// update basic auth
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *BasicAuthenticatorReconciler) ensureConfigmap(ctx context.Context, req
return r, err
}

authenticatorConfig := r.CreateNginxConfigmap(basicAuthenticator)
authenticatorConfig := createNginxConfigmap(basicAuthenticator)
var foundConfigmap corev1.ConfigMap
err := r.Get(ctx, types.NamespacedName{Name: authenticatorConfig.Name, Namespace: basicAuthenticator.Namespace}, &foundConfigmap)
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -188,7 +188,7 @@ func (r *BasicAuthenticatorReconciler) ensureDeployment(ctx context.Context, req
func (r *BasicAuthenticatorReconciler) CreateDeploymentAuthenticator(ctx context.Context, req ctrl.Request, basicAuthenticator *v1alpha1.BasicAuthenticator, authenticatorConfigName, secretName string) (*ctrl.Result, error) {
logger := log.FromContext(ctx)

newDeployment := r.CreateNginxDeployment(basicAuthenticator, authenticatorConfigName, secretName)
newDeployment := createNginxDeployment(basicAuthenticator, authenticatorConfigName, secretName, r.CustomConfig)
foundDeployment := &appv1.Deployment{}
err := r.Get(ctx, types.NamespacedName{Name: newDeployment.Name, Namespace: basicAuthenticator.Namespace}, foundDeployment)
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -266,7 +266,7 @@ func (r *BasicAuthenticatorReconciler) CreateDeploymentAuthenticator(ctx context

func (r *BasicAuthenticatorReconciler) CreateSidecarAuthenticator(ctx context.Context, req ctrl.Request, basicAuthenticator *v1alpha1.BasicAuthenticator, authenticatorConfigName, secretName string) (*ctrl.Result, error) {
logger := log.FromContext(ctx)
deploymentsToUpdate, err := r.Injector(ctx, basicAuthenticator, authenticatorConfigName, secretName)
deploymentsToUpdate, err := injector(ctx, basicAuthenticator, authenticatorConfigName, secretName, r.CustomConfig, r.Client)
if err != nil {
logger.Error(err, "failed to inject into deployments")
return subreconciler.RequeueWithError(err)
Expand Down

0 comments on commit 5d406b0

Please sign in to comment.