| 1 |
/* |
| 2 |
Copyright 2019 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package options |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
|
| 22 |
"github.com/spf13/pflag" |
| 23 |
"k8s.io/utils/path" |
| 24 |
|
| 25 |
"k8s.io/apiserver/pkg/server" |
| 26 |
"k8s.io/apiserver/pkg/server/egressselector" |
| 27 |
) |
| 28 |
|
| 29 |
// EgressSelectorOptions holds the api server egress selector options. |
| 30 |
// See https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/1281-network-proxy/README.md |
| 31 |
type EgressSelectorOptions struct { |
| 32 |
// ConfigFile is the file path with api-server egress selector configuration. |
| 33 |
ConfigFile string |
| 34 |
} |
| 35 |
|
| 36 |
// NewEgressSelectorOptions creates a new instance of EgressSelectorOptions |
| 37 |
// |
| 38 |
// The option is to point to a configuration file for egress/konnectivity. |
| 39 |
// This determines which types of requests use egress/konnectivity and how they use it. |
| 40 |
// If empty the API Server will attempt to connect directly using the network. |
| 41 |
func NewEgressSelectorOptions() *EgressSelectorOptions { |
| 42 |
return &EgressSelectorOptions{} |
| 43 |
} |
| 44 |
|
| 45 |
// AddFlags adds flags related to admission for a specific APIServer to the specified FlagSet |
| 46 |
func (o *EgressSelectorOptions) AddFlags(fs *pflag.FlagSet) { |
| 47 |
if o == nil { |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
fs.StringVar(&o.ConfigFile, "egress-selector-config-file", o.ConfigFile, |
| 52 |
"File with apiserver egress selector configuration.") |
| 53 |
} |
| 54 |
|
| 55 |
// ApplyTo adds the egress selector settings to the server configuration. |
| 56 |
// In case egress selector settings were not provided by a cluster-admin |
| 57 |
// they will be prepared from the recommended/default/no-op values. |
| 58 |
func (o *EgressSelectorOptions) ApplyTo(c *server.Config) error { |
| 59 |
if o == nil { |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
npConfig, err := egressselector.ReadEgressSelectorConfiguration(o.ConfigFile) |
| 64 |
if err != nil { |
| 65 |
return fmt.Errorf("failed to read egress selector config: %v", err) |
| 66 |
} |
| 67 |
errs := egressselector.ValidateEgressSelectorConfiguration(npConfig) |
| 68 |
if len(errs) > 0 { |
| 69 |
return fmt.Errorf("failed to validate egress selector configuration: %v", errs.ToAggregate()) |
| 70 |
} |
| 71 |
|
| 72 |
cs, err := egressselector.NewEgressSelector(npConfig) |
| 73 |
if err != nil { |
| 74 |
return fmt.Errorf("failed to setup egress selector with config %#v: %v", npConfig, err) |
| 75 |
} |
| 76 |
c.EgressSelector = cs |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
// Validate verifies flags passed to EgressSelectorOptions. |
| 81 |
func (o *EgressSelectorOptions) Validate() []error { |
| 82 |
if o == nil || o.ConfigFile == "" { |
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
errs := []error{} |
| 87 |
|
| 88 |
if exists, err := path.Exists(path.CheckFollowSymlink, o.ConfigFile); !exists || err != nil { |
| 89 |
errs = append(errs, fmt.Errorf("egress-selector-config-file %s does not exist", o.ConfigFile)) |
| 90 |
} |
| 91 |
|
| 92 |
return errs |
| 93 |
} |