Skip to content

Commit

Permalink
Rename annotations varaibles
Browse files Browse the repository at this point in the history
Clean admitter functions

Signed-off-by: Mustafa Abdelrahman <[email protected]>
  • Loading branch information
MustafaSaber committed Aug 9, 2023
1 parent 7053661 commit 19f3b12
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 58 deletions.
21 changes: 2 additions & 19 deletions cmd/webhook/admission/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,15 @@ func (iga *IngressAdmitter) name() string {

func (iga *IngressAdmitter) admit(req *admissionRequest) (*admissionResponse, error) {

// Serve as default validator if not set
if iga.IngressValidator == nil {
iga.IngressValidator = &definitions.IngressV1Validator{}
}

ingressItem := definitions.IngressV1Item{}
err := json.Unmarshal(req.Object, &ingressItem)
if err != nil {
return &admissionResponse{
UID: req.UID,
Allowed: false,
Result: &status{
Message: err.Error(),
},
}, err
return nil, err
}

err = iga.IngressValidator.Validate(&ingressItem)
if err != nil {
return &admissionResponse{
UID: req.UID,
Allowed: false,
Result: &status{
Message: err.Error(),
},
}, err
return nil, err
}

return &admissionResponse{
Expand Down
21 changes: 2 additions & 19 deletions cmd/webhook/admission/routegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,15 @@ func (rga *RouteGroupAdmitter) name() string {

func (rga *RouteGroupAdmitter) admit(req *admissionRequest) (*admissionResponse, error) {

// Serve as default validator if not set
if rga.RouteGroupValidator == nil {
rga.RouteGroupValidator = &definitions.RouteGroupValidator{}
}

rgItem := definitions.RouteGroupItem{}
err := json.Unmarshal(req.Object, &rgItem)
if err != nil {
return &admissionResponse{
UID: req.UID,
Allowed: false,
Result: &status{
Message: err.Error(),
},
}, err
return nil, err
}

err = rga.RouteGroupValidator.Validate(&rgItem)
if err != nil {
return &admissionResponse{
UID: req.UID,
Allowed: false,
Result: &status{
Message: err.Error(),
},
}, err
return nil, err
}

return &admissionResponse{
Expand Down
5 changes: 3 additions & 2 deletions dataclients/kubernetes/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type clusterClient struct {
routeGroupsLabelSelectors string

loggedMissingRouteGroups bool
routeGroupValidator *definitions.RouteGroupValidator
}

var (
Expand Down Expand Up @@ -162,6 +163,7 @@ func newClusterClient(o Options, apiURL, ingCls, rgCls string, quit <-chan struc
httpClient: httpClient,
apiURL: apiURL,
certificateRegistry: o.CertificateRegistry,
routeGroupValidator: &definitions.RouteGroupValidator{},
}

if o.KubernetesInCluster {
Expand Down Expand Up @@ -358,11 +360,10 @@ func (c *clusterClient) LoadRouteGroups() ([]*definitions.RouteGroupItem, error)
return nil, err
}

routeGroupValidator := &definitions.RouteGroupValidator{}
rgs := make([]*definitions.RouteGroupItem, 0, len(rgl.Items))
for _, i := range rgl.Items {
// Validate RouteGroup item.
if err := routeGroupValidator.Validate(i); err != nil {
if err := c.routeGroupValidator.Validate(i); err != nil {
log.Errorf("[routegroup] %v", err)
continue
}
Expand Down
6 changes: 3 additions & 3 deletions dataclients/kubernetes/definitions/ingressv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

const (
SkipperfilterAnnotationKey = "zalando.org/skipper-filter"
SkipperpredicateAnnotationKey = "zalando.org/skipper-predicate"
SkipperRoutesAnnotationKey = "zalando.org/skipper-routes"
IngressFilterAnnotation = "zalando.org/skipper-filter"
IngressPredicateAnnotation = "zalando.org/skipper-predicate"
IngressRoutesAnnotation = "zalando.org/skipper-routes"
)

var errInvalidPortType = errors.New("invalid port type")
Expand Down
12 changes: 6 additions & 6 deletions dataclients/kubernetes/definitions/ingressvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ func (igv *IngressV1Validator) Validate(item *IngressV1Item) error {
}

func (igv *IngressV1Validator) validateFilterAnnotation(annotations map[string]string) error {
if filters, ok := annotations[SkipperfilterAnnotationKey]; ok {
if filters, ok := annotations[IngressFilterAnnotation]; ok {
_, err := eskip.ParseFilters(filters)
if err != nil {
err = fmt.Errorf("invalid \"%s\" annotation: %w", SkipperfilterAnnotationKey, err)
err = fmt.Errorf("invalid \"%s\" annotation: %w", IngressFilterAnnotation, err)
}
return err
}
return nil
}

func (igv *IngressV1Validator) validatePredicateAnnotation(annotations map[string]string) error {
if predicates, ok := annotations[SkipperpredicateAnnotationKey]; ok {
if predicates, ok := annotations[IngressPredicateAnnotation]; ok {
_, err := eskip.ParsePredicates(predicates)
if err != nil {
err = fmt.Errorf("invalid \"%s\" annotation: %w", SkipperpredicateAnnotationKey, err)
err = fmt.Errorf("invalid \"%s\" annotation: %w", IngressPredicateAnnotation, err)
}
return err
}
return nil
}

func (igv *IngressV1Validator) validateRoutesAnnotation(annotations map[string]string) error {
if routes, ok := annotations[SkipperRoutesAnnotationKey]; ok {
if routes, ok := annotations[IngressRoutesAnnotation]; ok {
_, err := eskip.Parse(routes)
if err != nil {
err = fmt.Errorf("invalid \"%s\" annotation: %w", SkipperRoutesAnnotationKey, err)
err = fmt.Errorf("invalid \"%s\" annotation: %w", IngressRoutesAnnotation, err)
}
return err
}
Expand Down
8 changes: 4 additions & 4 deletions dataclients/kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func annotationFilter(m *definitions.Metadata, logger *logger) []*eskip.Filter {
if ratelimitAnnotationValue, ok := m.Annotations[ratelimitAnnotationKey]; ok {
annotationFilter = ratelimitAnnotationValue
}
if val, ok := m.Annotations[definitions.SkipperfilterAnnotationKey]; ok {
if val, ok := m.Annotations[definitions.IngressFilterAnnotation]; ok {
if annotationFilter != "" {
annotationFilter += " -> "
}
Expand All @@ -246,7 +246,7 @@ func annotationFilter(m *definitions.Metadata, logger *logger) []*eskip.Filter {
// parse predicate annotation
func annotationPredicate(m *definitions.Metadata) string {
var annotationPredicate string
if val, ok := m.Annotations[definitions.SkipperpredicateAnnotationKey]; ok {
if val, ok := m.Annotations[definitions.IngressPredicateAnnotation]; ok {
annotationPredicate = val
}
return annotationPredicate
Expand All @@ -255,12 +255,12 @@ func annotationPredicate(m *definitions.Metadata) string {
// parse routes annotation
func extraRoutes(m *definitions.Metadata, logger *logger) []*eskip.Route {
var extraRoutes []*eskip.Route
annotationRoutes := m.Annotations[definitions.SkipperRoutesAnnotationKey]
annotationRoutes := m.Annotations[definitions.IngressRoutesAnnotation]
if annotationRoutes != "" {
var err error
extraRoutes, err = eskip.Parse(annotationRoutes)
if err != nil {
logger.Errorf("Failed to parse routes from %s, skipping: %v", definitions.SkipperRoutesAnnotationKey, err)
logger.Errorf("Failed to parse routes from %s, skipping: %v", definitions.IngressRoutesAnnotation, err)
}
}
return extraRoutes
Expand Down
6 changes: 3 additions & 3 deletions dataclients/kubernetes/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ func testIngress(ns, name, defaultService, ratelimitCfg, filterString, predicate
setAnnotation(i, ratelimitAnnotationKey, ratelimitCfg)
}
if filterString != "" {
setAnnotation(i, definitions.SkipperfilterAnnotationKey, filterString)
setAnnotation(i, definitions.IngressFilterAnnotation, filterString)
}
if predicateString != "" {
setAnnotation(i, definitions.SkipperpredicateAnnotationKey, predicateString)
setAnnotation(i, definitions.IngressPredicateAnnotation, predicateString)
}
if routesString != "" {
setAnnotation(i, definitions.SkipperRoutesAnnotationKey, routesString)
setAnnotation(i, definitions.IngressRoutesAnnotation, routesString)
}
if pathModeString != "" {
setAnnotation(i, pathModeAnnotationKey, pathModeString)
Expand Down
2 changes: 1 addition & 1 deletion dataclients/kubernetes/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestPathMatchingModes(t *testing.T) {

annotation := strings.Join(annotations, " && ")
if len(annotations) > 0 {
i.Metadata.Annotations[definitions.SkipperpredicateAnnotationKey] = annotation
i.Metadata.Annotations[definitions.IngressPredicateAnnotation] = annotation
}

api.ingresses.Items = []*definitions.IngressV1Item{i}
Expand Down
2 changes: 1 addition & 1 deletion dataclients/kubernetes/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func createRedirectInfo(defaultEnabled bool, defaultCode int) *redirectInfo {
func (r *redirectInfo) initCurrent(m *definitions.Metadata) {
r.enable = m.Annotations[redirectAnnotationKey] == "true"
r.disable = m.Annotations[redirectAnnotationKey] == "false"
r.ignore = strings.Contains(m.Annotations[definitions.SkipperpredicateAnnotationKey], `Header("X-Forwarded-Proto"`) || strings.Contains(m.Annotations[definitions.SkipperRoutesAnnotationKey], `Header("X-Forwarded-Proto"`)
r.ignore = strings.Contains(m.Annotations[definitions.IngressPredicateAnnotation], `Header("X-Forwarded-Proto"`) || strings.Contains(m.Annotations[definitions.IngressRoutesAnnotation], `Header("X-Forwarded-Proto"`)

r.code = r.defaultCode
if annotationCode, ok := m.Annotations[redirectCodeAnnotationKey]; ok {
Expand Down

0 comments on commit 19f3b12

Please sign in to comment.