Skip to content

Commit

Permalink
refactored annotation endpoint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Jan 29, 2021
1 parent 5b0dd16 commit bd5ac48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions amp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Config struct {
Log *zap.Logger
HttpClient *http.Client
Cs *kubernetes.Clientset
EpAnnotation string
EpValidatingAnnotation string
MutationEpAnnotation string
ValidationEpAnnotation string
}

// Api
Expand Down Expand Up @@ -155,7 +155,7 @@ func (a *Api) validatePod(ar admissionv1.AdmissionReview) *admissionv1.Admission

logInfo := []zap.Field{
zap.String("namespace", ar.Request.Namespace),
zap.String("annotation", a.EpAnnotation),
zap.String("annotation", a.MutationEpAnnotation),
}

reviewResponse := admissionv1.AdmissionResponse{}
Expand Down Expand Up @@ -199,7 +199,7 @@ func (a *Api) validatePod(ar admissionv1.AdmissionReview) *admissionv1.Admission

// lookup endpoint by namespace annotation
annotations := ns.GetAnnotations()
ep, ok := annotations[a.EpValidatingAnnotation]
ep, ok := annotations[a.ValidationEpAnnotation]
if ok == false {
a.Log.Warn("DEFAULT ALLOW if no validation endpoint is configured for namespace.", logInfo...)
reviewResponse.Allowed = true
Expand All @@ -208,7 +208,7 @@ func (a *Api) validatePod(ar admissionv1.AdmissionReview) *admissionv1.Admission

logInfo = append(logInfo,
zap.String("endpoint", ep),
zap.String("annotation", a.EpValidatingAnnotation),
zap.String("annotation", a.ValidationEpAnnotation),
)

a.Log.Info("got validation endpoint from namespace", logInfo...)
Expand Down Expand Up @@ -303,7 +303,7 @@ func (a *Api) mutatePod(ar admissionv1.AdmissionReview) *admissionv1.AdmissionRe

logInfo := []zap.Field{
zap.String("namespace", ar.Request.Namespace),
zap.String("annotation", a.EpAnnotation),
zap.String("annotation", a.MutationEpAnnotation),
}

reviewResponse := admissionv1.AdmissionResponse{}
Expand Down Expand Up @@ -348,15 +348,15 @@ func (a *Api) mutatePod(ar admissionv1.AdmissionReview) *admissionv1.AdmissionRe

// lookup endpoint by namespace annotation
annotations := ns.GetAnnotations()
ep, ok := annotations[a.EpAnnotation]
ep, ok := annotations[a.MutationEpAnnotation]
if ok == false {
a.Log.Warn("no endpoint configured for namespace", logInfo...)
return &reviewResponse
}

logInfo = append(logInfo,
zap.String("endpoint", ep),
zap.String("annotation", a.EpAnnotation),
zap.String("annotation", a.MutationEpAnnotation),
)

a.Log.Info("got endpoint from namespace", logInfo...)
Expand Down
12 changes: 6 additions & 6 deletions cmd/amp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var (
httpReadTimeoutEnv = getEnv("HTTP_READ_TIMEOUT", "10")
httpWriteTimeoutEnv = getEnv("HTTP_WRITE_TIMEOUT", "10")
certPathEnv = getEnv("CERT_PATH", "")
epAnnotationEnv = getEnv("ANNOTATION_EP", "amp.txn2.com/ep")
epValidatingAnnotationEnv = getEnv("VALIDATING_ANNOTATION_EP", "amp.txn2.com/ep/validating")
mutationEpAnnotationEnv = getEnv("MUTATION_EP_ANNOTATION", "mutation.amp.txn2.com/ep")
validationEpAnnotationEnv = getEnv("VALIDATION_ANNOTATION_EP", "validation.amp.txn2.com/ep")
)

var Version = "0.0.0"
Expand Down Expand Up @@ -79,8 +79,8 @@ func main() {
mode = flag.String("mode", modeEnv, "debug or release")
httpReadTimeout = flag.Int("httpReadTimeout", httpReadTimeoutInt, "HTTP read timeout")
httpWriteTimeout = flag.Int("httpWriteTimeout", httpWriteTimeoutInt, "HTTP write timeout")
epAnnotation = flag.String("epAnnotation", epAnnotationEnv, "Endpoint annotation")
epValidatingAnnotation = flag.String("epValidatingAnnotation", epValidatingAnnotationEnv, "Endpoint annotation for validating")
mutationEpAnnotation = flag.String("mutationEpAnnotation", mutationEpAnnotationEnv, "Mutation endpoint annotation")
validationEpAnnotation = flag.String("validationEpAnnotation", validationEpAnnotationEnv, "Validation endpoint annotation")
)
flag.Parse()

Expand Down Expand Up @@ -174,8 +174,8 @@ func main() {
Log: logger,
HttpClient: httpClient,
Cs: cs,
EpAnnotation: *epAnnotation,
EpValidatingAnnotation: *epValidatingAnnotation,
MutationEpAnnotation: *mutationEpAnnotation,
ValidationEpAnnotation: *validationEpAnnotation,
})
if err != nil {
logger.Fatal("Error getting API.", zap.Error(err))
Expand Down

0 comments on commit bd5ac48

Please sign in to comment.