Skip to content

Commit

Permalink
UPSTREAM: <carry>: kube-controller-manager: allow running bare kube-c…
Browse files Browse the repository at this point in the history
…ontroller-manager

UPSTREAM: <carry>: (squash) kube-controller-manager: allow running bare kube-controller-manager

UPSTREAM: <carry>: kube-controller-manager: allow running bare kube-controller-manager

UPSTREAM: <carry>: (squash) remove egressnetworkpolicies from gc ignored resources

egressnetworkpolicies should not be in garbage collector ignored
resources, so users can delete them using "--cascade=foreground" flag.

Signed-off-by: Flavio Fernandes <flaviof@redhat.com>

OpenShift-Rebase-Source: 6c1dee4
  • Loading branch information
deads2k authored and bertinatto committed Apr 11, 2023
1 parent 337baad commit ca4b513
Show file tree
Hide file tree
Showing 12 changed files with 842 additions and 11 deletions.
6 changes: 4 additions & 2 deletions cmd/kube-controller-manager/app/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import (
)

func startDaemonSetController(ctx context.Context, controllerContext ControllerContext) (controller.Interface, bool, error) {
dsc, err := daemon.NewDaemonSetsController(
ctx,
dsc, err := daemon.NewNodeSelectorAwareDaemonSetsController(
controllerContext.OpenShiftContext.OpenShiftDefaultProjectNodeSelector,
controllerContext.OpenShiftContext.KubeDefaultProjectNodeSelector,
controllerContext.InformerFactory.Core().V1().Namespaces(),
controllerContext.InformerFactory.Apps().V1().DaemonSets(),
controllerContext.InformerFactory.Apps().V1().ControllerRevisions(),
controllerContext.InformerFactory.Core().V1().Pods(),
Expand Down
2 changes: 2 additions & 0 deletions cmd/kube-controller-manager/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

// Config is the main context object for the controller manager.
type Config struct {
OpenShiftContext OpenShiftContext

ComponentConfig kubectrlmgrconfig.KubeControllerManagerConfiguration

SecureServing *apiserver.SecureServingInfo
Expand Down
9 changes: 9 additions & 0 deletions cmd/kube-controller-manager/app/config/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

// OpenShiftContext is additional context that we need to launch the kube-controller-manager for openshift.
// Basically, this holds our additional config information.
type OpenShiftContext struct {
OpenShiftConfig string
OpenShiftDefaultProjectNodeSelector string
KubeDefaultProjectNodeSelector string
}
20 changes: 17 additions & 3 deletions cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ controller, and serviceaccounts controller.`,
if err != nil {
return err
}

if err := ShimForOpenShift(s, c); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
}

// add feature enablement metrics
utilfeature.DefaultMutableFeatureGate.AddMetrics()
return Run(context.Background(), c.Complete())
Expand Down Expand Up @@ -333,6 +339,8 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {

// ControllerContext defines the context object for controller
type ControllerContext struct {
OpenShiftContext config.OpenShiftContext

// ClientBuilder will provide a client for this controller to use
ClientBuilder clientbuilder.ControllerClientBuilder

Expand Down Expand Up @@ -521,7 +529,12 @@ func GetAvailableResources(clientBuilder clientbuilder.ControllerClientBuilder)
// the shared-informers client and token controller.
func CreateControllerContext(logger klog.Logger, s *config.CompletedConfig, rootClientBuilder, clientBuilder clientbuilder.ControllerClientBuilder, stop <-chan struct{}) (ControllerContext, error) {
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
sharedInformers := informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
var sharedInformers informers.SharedInformerFactory
if InformerFactoryOverride == nil {
sharedInformers = informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
} else {
sharedInformers = InformerFactoryOverride
}

metadataClient := metadata.NewForConfigOrDie(rootClientBuilder.ConfigOrDie("metadata-informers"))
metadataInformers := metadatainformer.NewSharedInformerFactory(metadataClient, ResyncPeriod(s)())
Expand Down Expand Up @@ -552,6 +565,7 @@ func CreateControllerContext(logger klog.Logger, s *config.CompletedConfig, root
}

ctx := ControllerContext{
OpenShiftContext: s.OpenShiftContext,
ClientBuilder: clientBuilder,
InformerFactory: sharedInformers,
ObjectOrMetadataInformerFactory: informerfactory.NewInformerFactory(sharedInformers, metadataInformers),
Expand Down Expand Up @@ -683,10 +697,10 @@ func (c serviceAccountTokenControllerStarter) startServiceAccountTokenController
controllerContext.InformerFactory.Core().V1().ServiceAccounts(),
controllerContext.InformerFactory.Core().V1().Secrets(),
c.rootClientBuilder.ClientOrDie("tokens-controller"),
serviceaccountcontroller.TokensControllerOptions{
applyOpenShiftServiceServingCertCA(serviceaccountcontroller.TokensControllerOptions{
TokenGenerator: tokenGenerator,
RootCA: rootCA,
},
}),
)
if err != nil {
return nil, true, fmt.Errorf("error creating Tokens controller: %v", err)
Expand Down
6 changes: 6 additions & 0 deletions cmd/kube-controller-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type KubeControllerManagerOptions struct {

Master string
ShowHiddenMetricsForVersion string
OpenShiftContext kubecontrollerconfig.OpenShiftContext
}

// NewKubeControllerManagerOptions creates a new KubeControllerManagerOptions with a default config.
Expand Down Expand Up @@ -260,6 +261,8 @@ func (s *KubeControllerManagerOptions) Flags(allControllers []string, disabledBy
fs := fss.FlagSet("misc")
fs.StringVar(&s.Master, "master", s.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig).")
fs.StringVar(&s.Generic.ClientConnection.Kubeconfig, "kubeconfig", s.Generic.ClientConnection.Kubeconfig, "Path to kubeconfig file with authorization and master location information (the master location can be overridden by the master flag).")
fs.StringVar(&s.OpenShiftContext.OpenShiftConfig, "openshift-config", s.OpenShiftContext.OpenShiftConfig, "indicates that this process should be compatible with openshift start master")
fs.MarkHidden("openshift-config")
utilfeature.DefaultMutableFeatureGate.AddFlag(fss.FlagSet("generic"))

return fss
Expand Down Expand Up @@ -359,6 +362,9 @@ func (s *KubeControllerManagerOptions) ApplyTo(c *kubecontrollerconfig.Config) e
return err
}
}

c.OpenShiftContext = s.OpenShiftContext

return nil
}

Expand Down
84 changes: 84 additions & 0 deletions cmd/kube-controller-manager/app/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package app

import (
"io/ioutil"
"path"

"k8s.io/apimachinery/pkg/util/json"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/informers"
"k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
"k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
)

var InformerFactoryOverride informers.SharedInformerFactory

func ShimForOpenShift(controllerManagerOptions *options.KubeControllerManagerOptions, controllerManager *config.Config) error {
if len(controllerManager.OpenShiftContext.OpenShiftConfig) == 0 {
return nil
}

// TODO this gets removed when no longer take flags and no longer build a recycler template
openshiftConfig, err := getOpenShiftConfig(controllerManager.OpenShiftContext.OpenShiftConfig)
if err != nil {
return err
}

// TODO this should be replaced by using a flex volume to inject service serving cert CAs into pods instead of adding it to the sa token
if err := applyOpenShiftServiceServingCertCAFunc(path.Dir(controllerManager.OpenShiftContext.OpenShiftConfig), openshiftConfig); err != nil {
return err
}

// skip GC on some openshift resources
// TODO this should be replaced by discovery information in some way
if err := applyOpenShiftGCConfig(controllerManager); err != nil {
return err
}

if err := applyOpenShiftConfigDefaultProjectSelector(controllerManagerOptions, openshiftConfig); err != nil {
return err
}

// Overwrite the informers, because we have our custom generic informers for quota.
// TODO update quota to create its own informer like garbage collection
if informers, err := newInformerFactory(controllerManager.Kubeconfig); err != nil {
return err
} else {
InformerFactoryOverride = informers
}

return nil
}

func getOpenShiftConfig(configFile string) (map[string]interface{}, error) {
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
jsonBytes, err := kyaml.ToJSON(configBytes)
if err != nil {
return nil, err
}
config := map[string]interface{}{}
if err := json.Unmarshal(jsonBytes, &config); err != nil {
return nil, err
}

return config, nil
}

func applyOpenShiftConfigDefaultProjectSelector(controllerManagerOptions *options.KubeControllerManagerOptions, openshiftConfig map[string]interface{}) error {
projectConfig, ok := openshiftConfig["projectConfig"]
if !ok {
return nil
}

castProjectConfig := projectConfig.(map[string]interface{})
defaultNodeSelector, ok := castProjectConfig["defaultNodeSelector"]
if !ok {
return nil
}
controllerManagerOptions.OpenShiftContext.OpenShiftDefaultProjectNodeSelector = defaultNodeSelector.(string)

return nil
}
39 changes: 39 additions & 0 deletions cmd/kube-controller-manager/app/patch_gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package app

import (
gcconfig "k8s.io/kubernetes/pkg/controller/garbagecollector/config"

"k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
)

func applyOpenShiftGCConfig(controllerManager *config.Config) error {
// TODO make this configurable or discoverable. This is going to prevent us from running the stock GC controller
// IF YOU ADD ANYTHING TO THIS LIST, MAKE SURE THAT YOU UPDATE THEIR STRATEGIES TO PREVENT GC FINALIZERS
controllerManager.ComponentConfig.GarbageCollectorController.GCIgnoredResources = append(controllerManager.ComponentConfig.GarbageCollectorController.GCIgnoredResources,
// explicitly disabled from GC for now - not enough value to track them
gcconfig.GroupResource{Group: "authorization.openshift.io", Resource: "rolebindingrestrictions"},
gcconfig.GroupResource{Group: "network.openshift.io", Resource: "clusternetworks"},
gcconfig.GroupResource{Group: "network.openshift.io", Resource: "hostsubnets"},
gcconfig.GroupResource{Group: "network.openshift.io", Resource: "netnamespaces"},
gcconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthclientauthorizations"},
gcconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthclients"},
gcconfig.GroupResource{Group: "quota.openshift.io", Resource: "clusterresourcequotas"},
gcconfig.GroupResource{Group: "user.openshift.io", Resource: "groups"},
gcconfig.GroupResource{Group: "user.openshift.io", Resource: "identities"},
gcconfig.GroupResource{Group: "user.openshift.io", Resource: "users"},
gcconfig.GroupResource{Group: "image.openshift.io", Resource: "images"},

// virtual resource
gcconfig.GroupResource{Group: "project.openshift.io", Resource: "projects"},
// virtual and unwatchable resource, surfaced via rbac.authorization.k8s.io objects
gcconfig.GroupResource{Group: "authorization.openshift.io", Resource: "clusterroles"},
gcconfig.GroupResource{Group: "authorization.openshift.io", Resource: "clusterrolebindings"},
gcconfig.GroupResource{Group: "authorization.openshift.io", Resource: "roles"},
gcconfig.GroupResource{Group: "authorization.openshift.io", Resource: "rolebindings"},
// these resources contain security information in their names, and we don't need to track them
gcconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthaccesstokens"},
gcconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthauthorizetokens"},
)

return nil
}

0 comments on commit ca4b513

Please sign in to comment.