/
ingress_opts.go
76 lines (66 loc) · 2.67 KB
/
ingress_opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cmd
import (
"fmt"
validate "github.com/go-playground/validator/v10"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/types"
icsv1 "github.com/pomerium/ingress-controller/apis/ingress/v1"
"github.com/pomerium/ingress-controller/controllers/ingress"
"github.com/pomerium/ingress-controller/util"
)
type ingressControllerOpts struct {
ClassName string `validate:"required"`
AnnotationPrefix string `validate:"required"`
Namespaces []string
UpdateStatusFromService string ``
GlobalSettings string `validate:"required"`
}
const (
ingressClassControllerName = "name"
annotationPrefix = "prefix"
namespaces = "namespaces"
sharedSecret = "shared-secret"
updateStatusFromService = "update-status-from-service"
globalSettings = "pomerium-config"
)
func (s *ingressControllerOpts) setupFlags(flags *pflag.FlagSet) {
flags.StringVar(&s.ClassName, ingressClassControllerName, ingress.DefaultClassControllerName, "IngressClass controller name")
flags.StringVar(&s.AnnotationPrefix, annotationPrefix, ingress.DefaultAnnotationPrefix, "Ingress annotation prefix")
flags.StringSliceVar(&s.Namespaces, namespaces, nil, "namespaces to watch, or none to watch all namespaces")
flags.StringVar(&s.UpdateStatusFromService, updateStatusFromService, "", "update ingress status from given service status (pomerium-proxy)")
flags.StringVar(&s.GlobalSettings, globalSettings, "",
fmt.Sprintf("namespace/name to a resource of type %s/Settings", icsv1.GroupVersion.Group))
}
func (s *ingressControllerOpts) Validate() error {
return validate.New().Struct(s)
}
func (s *ingressControllerOpts) getGlobalSettings() (*types.NamespacedName, error) {
if s.GlobalSettings == "" {
return nil, nil
}
name, err := util.ParseNamespacedName(s.GlobalSettings, util.WithClusterScope())
if err != nil {
return nil, fmt.Errorf("%s=%s: %w", globalSettings, s.GlobalSettings, err)
}
return name, nil
}
func (s *ingressControllerOpts) getIngressControllerOptions() ([]ingress.Option, error) {
opts := []ingress.Option{
ingress.WithNamespaces(s.Namespaces),
ingress.WithAnnotationPrefix(s.AnnotationPrefix),
ingress.WithControllerName(s.ClassName),
}
if name, err := s.getGlobalSettings(); err != nil {
return nil, err
} else if name != nil {
opts = append(opts, ingress.WithGlobalSettings(*name))
}
if s.UpdateStatusFromService != "" {
name, err := util.ParseNamespacedName(s.UpdateStatusFromService)
if err != nil {
return nil, fmt.Errorf("update status from service: %q: %w", s.UpdateStatusFromService, err)
}
opts = append(opts, ingress.WithUpdateIngressStatusFromService(*name))
}
return opts, nil
}