| 1 |
/* |
| 2 |
Copyright 2017 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 |
"strings" |
| 22 |
|
| 23 |
"github.com/spf13/pflag" |
| 24 |
|
| 25 |
"k8s.io/apiserver/pkg/server" |
| 26 |
"k8s.io/apiserver/pkg/server/resourceconfig" |
| 27 |
serverstore "k8s.io/apiserver/pkg/server/storage" |
| 28 |
cliflag "k8s.io/component-base/cli/flag" |
| 29 |
) |
| 30 |
|
| 31 |
// APIEnablementOptions contains the options for which resources to turn on and off. |
| 32 |
// Given small aggregated API servers, this option isn't required for "normal" API servers |
| 33 |
type APIEnablementOptions struct { |
| 34 |
RuntimeConfig cliflag.ConfigurationMap |
| 35 |
} |
| 36 |
|
| 37 |
func NewAPIEnablementOptions() *APIEnablementOptions { |
| 38 |
return &APIEnablementOptions{ |
| 39 |
RuntimeConfig: make(cliflag.ConfigurationMap), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// AddFlags adds flags for a specific APIServer to the specified FlagSet |
| 44 |
func (s *APIEnablementOptions) AddFlags(fs *pflag.FlagSet) { |
| 45 |
if s == nil { |
| 46 |
return |
| 47 |
} |
| 48 |
fs.Var(&s.RuntimeConfig, "runtime-config", ""+ |
| 49 |
"A set of key=value pairs that enable or disable built-in APIs. Supported options are:\n"+ |
| 50 |
"v1=true|false for the core API group\n"+ |
| 51 |
"<group>/<version>=true|false for a specific API group and version (e.g. apps/v1=true)\n"+ |
| 52 |
"api/all=true|false controls all API versions\n"+ |
| 53 |
"api/ga=true|false controls all API versions of the form v[0-9]+\n"+ |
| 54 |
"api/beta=true|false controls all API versions of the form v[0-9]+beta[0-9]+\n"+ |
| 55 |
"api/alpha=true|false controls all API versions of the form v[0-9]+alpha[0-9]+\n"+ |
| 56 |
"api/legacy is deprecated, and will be removed in a future version") |
| 57 |
} |
| 58 |
|
| 59 |
// Validate validates RuntimeConfig with a list of registries. |
| 60 |
// Usually this list only has one element, the apiserver registry of the process. |
| 61 |
// But in the advanced (and usually not recommended) case of delegated apiservers there can be more. |
| 62 |
// Validate will filter out the known groups of each registry. |
| 63 |
// If anything is left over after that, an error is returned. |
| 64 |
func (s *APIEnablementOptions) Validate(registries ...GroupRegistry) []error { |
| 65 |
if s == nil { |
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
errors := []error{} |
| 70 |
if s.RuntimeConfig[resourceconfig.APIAll] == "false" && len(s.RuntimeConfig) == 1 { |
| 71 |
// Do not allow only set api/all=false, in such case apiserver startup has no meaning. |
| 72 |
return append(errors, fmt.Errorf("invalid key with only %v=false", resourceconfig.APIAll)) |
| 73 |
} |
| 74 |
|
| 75 |
groups, err := resourceconfig.ParseGroups(s.RuntimeConfig) |
| 76 |
if err != nil { |
| 77 |
return append(errors, err) |
| 78 |
} |
| 79 |
|
| 80 |
for _, registry := range registries { |
| 81 |
// filter out known groups |
| 82 |
groups = unknownGroups(groups, registry) |
| 83 |
} |
| 84 |
if len(groups) != 0 { |
| 85 |
errors = append(errors, fmt.Errorf("unknown api groups %s", strings.Join(groups, ","))) |
| 86 |
} |
| 87 |
|
| 88 |
return errors |
| 89 |
} |
| 90 |
|
| 91 |
// ApplyTo override MergedResourceConfig with defaults and registry |
| 92 |
func (s *APIEnablementOptions) ApplyTo(c *server.Config, defaultResourceConfig *serverstore.ResourceConfig, registry resourceconfig.GroupVersionRegistry) error { |
| 93 |
if s == nil { |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
mergedResourceConfig, err := resourceconfig.MergeAPIResourceConfigs(defaultResourceConfig, s.RuntimeConfig, registry) |
| 98 |
if err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
// apply emulation forward compatibility to the api enablement if applicable. |
| 102 |
if c.EmulationForwardCompatible { |
| 103 |
mergedResourceConfig, err = resourceconfig.EmulationForwardCompatibleResourceConfig(mergedResourceConfig, s.RuntimeConfig, registry) |
| 104 |
} |
| 105 |
|
| 106 |
c.MergedResourceConfig = mergedResourceConfig |
| 107 |
|
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
func unknownGroups(groups []string, registry GroupRegistry) []string { |
| 112 |
unknownGroups := []string{} |
| 113 |
for _, group := range groups { |
| 114 |
if !registry.IsGroupRegistered(group) { |
| 115 |
unknownGroups = append(unknownGroups, group) |
| 116 |
} |
| 117 |
} |
| 118 |
return unknownGroups |
| 119 |
} |
| 120 |
|
| 121 |
// GroupRegistry provides a method to check whether given group is registered. |
| 122 |
type GroupRegistry interface { |
| 123 |
// IsRegistered returns true if given group is registered. |
| 124 |
IsGroupRegistered(group string) bool |
| 125 |
} |