Skip to content

Instantly share code, notes, and snippets.

@harsha89
Last active April 14, 2020 09:57
Show Gist options
  • Save harsha89/809da1f7e602e242eba90216330fc04b to your computer and use it in GitHub Desktop.
Save harsha89/809da1f7e602e242eba90216330fc04b to your computer and use it in GitHub Desktop.
// Creating an Ingress resource
func createorUpdateIngressResource(r *ReconcileAPI, cr *wso2v1alpha1.API, httpPortVal int32, httpsPortVal int32,
apiBasePathMap map[string]string, controllerConfig *corev1.ConfigMap, owner []metav1.OwnerReference) error {
controlConfigData := controllerConfig.Data
transportMode := //What's the transport mode of the ingress whether http or https
ingressHostName := //Hostname to be used for the ingress
tlsSecretName := //tls secret
ingressNamePrefix := //name prefix for the ingress
ingressName := //ingress name
namespace := //namespace
apiServiceName := //serivce name to be exposed
var hostArray []string
hostArray = append(hostArray, ingressHostName)
log.Info(fmt.Sprintf("Creating ingress resource with name: %v", ingressName))
log.WithValues("Ingress metadata. Transport mode", transportMode, "Ingress name", ingressName,
"Ingress hostname ", ingressHostName)
annotationMap, err := getConfigmap(r, ingressConfigs, wso2NameSpaceConst)
var port int32
//Decide port based on the http or https (443 or 80)
if httpConst == transportMode {
port = 80
} else {
port = 443
}
//If you include ingress annotations via a config map. Here it's extracting and put these data
annotationConfigData := annotationMap.Data
annotationsList := annotationConfigData[ingressProperties]
var ingressAnnotationMap map[string]string
ingressAnnotationMap = make(map[string]string)
splitArray := strings.Split(annotationsList, "\n")
for _, element := range splitArray {
if element != "" && strings.ContainsAny(element, ":") {
splitValues := strings.Split(element, ":")
ingressAnnotationMap[strings.TrimSpace(splitValues[0])] = strings.TrimSpace(splitValues[1])
}
}
log.Info("Creating ingress resource with the following Base Paths")
// add multiple api base paths
var httpIngressPaths []v1beta1.HTTPIngressPath
for basePath := range apiBasePathMap {
apiBasePath := basePath
// if the base path contains /petstore/{version}, then it is converted to /petstore/1.0.0
if strings.Contains(basePath, versionField) {
apiBasePath = strings.Replace(basePath, versionField, apiBasePathMap[basePath], -1)
}
log.Info(fmt.Sprintf("Adding the base path: %v to ingress resource", apiBasePath))
httpIngressPaths = append(httpIngressPaths, v1beta1.HTTPIngressPath{
Path: apiBasePath,
Backend: v1beta1.IngressBackend{
ServiceName: apiServiceName,
ServicePort: intstr.IntOrString{IntVal: port},
},
})
}
ingressResource := &v1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace, // goes into backend full name
Name: ingressName,
Annotations: ingressAnnotationMap,
OwnerReferences: owner,
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{
Host: ingressHostName,
IngressRuleValue: v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: httpIngressPaths,
},
},
},
},
TLS: []v1beta1.IngressTLS{
{
Hosts: hostArray,
SecretName: tlsSecretName,
},
},
},
}
ingress := &v1beta1.Ingress{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: ingressName, Namespace: namespace}, ingress)
if err != nil && errors.IsNotFound(err) {
log.Info("Ingress resource not found with name " + ingressName + ".Hence creating a new Ingress resource")
err = r.client.Create(context.TODO(), ingressResource)
return err
} else {
log.Info("Ingress resource found with name " + ingressName + ".Hence updating the existing Ingress resource")
err = r.client.Update(context.TODO(), ingressResource)
return err
}
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment