Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generic 2-way merge handler. #26

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 29 additions & 82 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ required = [ "k8s.io/code-generator/cmd/client-gen", "k8s.io/gengo/types" ]
name = "k8s.io/code-generator"
version = "kubernetes-1.11.1"

[[constraint]]
name = "k8s.io/kube-aggregator"
version = "kubernetes-1.11.1"

[[constraint]]
name = "k8s.io/utils"
revision = "045dc31ee5c40e8240241ce28dc24d7b56130373"
Expand All @@ -62,3 +66,4 @@ required = [ "k8s.io/code-generator/cmd/client-gen", "k8s.io/gengo/types" ]
[[constraint]]
name = "github.com/openshift/client-go"
branch = "master"

4 changes: 2 additions & 2 deletions lib/resourcebuilder/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (b *deploymentBuilder) Do() error {
if b.modifier != nil {
b.modifier(deployment)
}
_, updated, err := resourceapply.ApplyDeployment(b.client, deployment)
actual, updated, err := resourceapply.ApplyDeployment(b.client, deployment)
if err != nil {
return err
}
if updated {
if updated && actual.Generation > 1 {
return waitForDeploymentCompletion(b.client, deployment)
}
return nil
Expand Down
36 changes: 33 additions & 3 deletions lib/resourcemerge/core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resourcemerge

import (
"reflect"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
)
Expand Down Expand Up @@ -94,9 +96,10 @@ func ensureContainer(modified *bool, existing *corev1.Container, required corev1
setStringIfSet(modified, &existing.Image, required.Image)

// if you want modify the launch, you need to modify it in the config, not in the launch args
setStringSliceIfSet(modified, &existing.Command, required.Command)
setStringSliceIfSet(modified, &existing.Args, required.Args)

setStringSlice(modified, &existing.Command, required.Command)
setStringSlice(modified, &existing.Args, required.Args)
ensureEnvVar(modified, &existing.Env, required.Env)
ensureEnvFromSource(modified, &existing.EnvFrom, required.EnvFrom)
setStringIfSet(modified, &existing.WorkingDir, required.WorkingDir)

// any port we specify, we require
Expand Down Expand Up @@ -144,6 +147,26 @@ func ensureContainer(modified *bool, existing *corev1.Container, required corev1
ensureSecurityContextPtr(modified, &existing.SecurityContext, required.SecurityContext)
}

func ensureEnvVar(modified *bool, existing *[]corev1.EnvVar, required []corev1.EnvVar) {
if required == nil {
return
}
if !equality.Semantic.DeepEqual(required, *existing) {
*existing = required
*modified = true
}
}

func ensureEnvFromSource(modified *bool, existing *[]corev1.EnvFromSource, required []corev1.EnvFromSource) {
if required == nil {
return
}
if !equality.Semantic.DeepEqual(required, *existing) {
*existing = required
*modified = true
}
}

func ensureProbePtr(modified *bool, existing **corev1.Probe, required *corev1.Probe) {
// if we have no required, then we don't care what someone else has set
if required == nil {
Expand Down Expand Up @@ -271,6 +294,13 @@ func setStringSliceIfSet(modified *bool, existing *[]string, required []string)
}
}

func setStringSlice(modified *bool, existing *[]string, required []string) {
if !reflect.DeepEqual(required, *existing) {
*existing = required
*modified = true
}
}

func mergeStringSlice(modified *bool, existing *[]string, required []string) {
for _, required := range required {
found := false
Expand Down
90 changes: 90 additions & 0 deletions pkg/cvo/internal/dynamicclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package dynamicclient

import (
"fmt"
"sync"
"time"

"k8s.io/apimachinery/pkg/api/meta"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)

type resourceClientFactory struct {
dynamicClient dynamic.Interface
restMapper *restmapper.DeferredDiscoveryRESTMapper
}

var (
// this stores the singleton in a package local
singletonFactory *resourceClientFactory
once sync.Once
)

// Private constructor for once.Do
func newSingletonFactory(config *rest.Config) func() {
return func() {
cachedDiscoveryClient := cached.NewMemCacheClient(kubernetes.NewForConfigOrDie(config).Discovery())
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient)
restMapper.Reset()

dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err)
}

singletonFactory = &resourceClientFactory{
dynamicClient: dynamicClient,
restMapper: restMapper,
}
singletonFactory.runBackgroundCacheReset(1 * time.Minute)
}
}

// New returns the resource client using a singleton factory
func New(config *rest.Config, gvk schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) {
once.Do(newSingletonFactory(config))
return singletonFactory.getResourceClient(gvk, namespace)
}

// getResourceClient returns the dynamic client for the resource specified by the gvk.
func (c *resourceClientFactory) getResourceClient(gvk schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) {
gvr, namespaced, err := gvkToGVR(gvk, c.restMapper)
if err != nil {
return nil, fmt.Errorf("failed to get resource type: %v", err)
}

// sometimes manifests of non-namespaced resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is formatted strangely.

// might have namespace set.
// preventing such cases.
ns := namespace
if !namespaced {
ns = ""
}
return c.dynamicClient.Resource(*gvr).Namespace(ns), nil
}

func gvkToGVR(gvk schema.GroupVersionKind, restMapper *restmapper.DeferredDiscoveryRESTMapper) (*schema.GroupVersionResource, bool, error) {
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, false, fmt.Errorf("failed to get the resource REST mapping for GroupVersionKind(%s): %v", gvk.String(), err)
}

return &mapping.Resource, mapping.Scope.Name() == meta.RESTScopeNameNamespace, nil
}

// runBackgroundCacheReset - Starts the rest mapper cache reseting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: resetting?

// at a duration given.
func (c *resourceClientFactory) runBackgroundCacheReset(duration time.Duration) {
ticker := time.NewTicker(duration)
go func() {
for range ticker.C {
c.restMapper.Reset()
}
}()
}