-
Notifications
You must be signed in to change notification settings - Fork 239
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
Convert cluster.openshift.io/Network in to the NetworkConfig CRD; update Network status #47
Merged
openshift-merge-robot
merged 3 commits into
openshift:master
from
squeed:cluster-config
Jan 15, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/openshift/cluster-network-operator/pkg/controller/clusterconfig" | ||
"github.com/openshift/cluster-network-operator/pkg/controller/networkconfig" | ||
) | ||
|
||
func init() { | ||
// AddToManagerFuncs is a list of functions to create controllers and add them to a manager. | ||
AddToManagerFuncs = append(AddToManagerFuncs, networkconfig.Add) | ||
AddToManagerFuncs = append(AddToManagerFuncs, | ||
networkconfig.Add, | ||
clusterconfig.Add, | ||
) | ||
} |
113 changes: 113 additions & 0 deletions
113
pkg/controller/clusterconfig/clusterconfig_controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/pkg/errors" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
"github.com/openshift/cluster-network-operator/pkg/apply" | ||
"github.com/openshift/cluster-network-operator/pkg/names" | ||
"github.com/openshift/cluster-network-operator/pkg/network" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// and Start it when the Manager is Started. | ||
func Add(mgr manager.Manager) error { | ||
return add(mgr, newReconciler(mgr)) | ||
} | ||
|
||
// newReconciler returns a new reconcile.Reconciler | ||
func newReconciler(mgr manager.Manager) reconcile.Reconciler { | ||
configv1.Install(mgr.GetScheme()) | ||
return &ReconcileClusterConfig{client: mgr.GetClient(), scheme: mgr.GetScheme()} | ||
} | ||
|
||
// add adds a new Controller to mgr with r as the reconcile.Reconciler | ||
func add(mgr manager.Manager, r reconcile.Reconciler) error { | ||
// Create a new controller | ||
c, err := controller.New("clusterconfig-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Watch for changes to primary resource config.openshift.io/v1/Network | ||
err = c.Watch(&source.Kind{Type: &configv1.Network{}}, &handler.EnqueueRequestForObject{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var _ reconcile.Reconciler = &ReconcileClusterConfig{} | ||
|
||
// ReconcileClusterConfig reconciles a cluster Network object | ||
type ReconcileClusterConfig struct { | ||
// This client, initialized using mgr.Client() above, is a split client | ||
// that reads objects from the cache and writes to the apiserver | ||
client client.Client | ||
scheme *runtime.Scheme | ||
} | ||
|
||
// Reconcile propagates changes from the cluster config to the operator config. | ||
// In other words, it watches Network.config.openshift.io/v1/cluster and updates | ||
// NetworkConfig.networkoperator.openshift.io/v1/default. | ||
func (r *ReconcileClusterConfig) Reconcile(request reconcile.Request) (reconcile.Result, error) { | ||
log.Printf("Reconciling Network.config.openshift.io %s\n", request.Name) | ||
|
||
// We won't create more than one network | ||
if request.Name != names.CLUSTER_CONFIG { | ||
log.Printf("Ignoring Network without default name " + names.CLUSTER_CONFIG) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Fetch the cluster config | ||
clusterConfig := &configv1.Network{} | ||
err := r.client.Get(context.TODO(), request.NamespacedName, clusterConfig) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
// Request object not found, could have been deleted after reconcile request. | ||
// Return and don't requeue | ||
log.Println("Object seems to have been deleted") | ||
return reconcile.Result{}, nil | ||
} | ||
// Error reading the object - requeue the request. | ||
squeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Println(err) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// Validate the cluster config | ||
if err := network.ValidateClusterConfig(clusterConfig.Spec); err != nil { | ||
err = errors.Wrapf(err, "failed to validate Network.Spec") | ||
log.Println(err) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
operatorConfig, err := r.UpdateOperatorConfig(context.TODO(), *clusterConfig) | ||
if err != nil { | ||
err = errors.Wrapf(err, "failed to generate NetworkConfig CRD") | ||
log.Println(err) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
if operatorConfig != nil { | ||
if err := apply.ApplyObject(context.TODO(), r.client, operatorConfig); err != nil { | ||
err = errors.Wrapf(err, "could not apply (%s) %s/%s", operatorConfig.GroupVersionKind(), operatorConfig.GetNamespace(), operatorConfig.GetName()) | ||
log.Println(err) | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
|
||
log.Printf("successfully updated ClusterNetwork (%s) %s/%s", operatorConfig.GroupVersionKind(), operatorConfig.GetNamespace(), operatorConfig.GetName()) | ||
return reconcile.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"github.com/pkg/errors" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
netopv1 "github.com/openshift/cluster-network-operator/pkg/apis/networkoperator/v1" | ||
"github.com/openshift/cluster-network-operator/pkg/names" | ||
"github.com/openshift/cluster-network-operator/pkg/network" | ||
k8sutil "github.com/openshift/cluster-network-operator/pkg/util/k8s" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// UpdateOperatorConfig merges the cluster network configuration in to the | ||
// operator configuration. | ||
// The operator's CRD is necessarily much more complicated, and 99% of users | ||
// will not need to create or touch it. So, they can touch the Network config. | ||
// Any changes in the cluster config will be noticed by the operator and merged | ||
// in to the operator CRD. | ||
// This returns nil if no changes have been detected. | ||
func (r *ReconcileClusterConfig) UpdateOperatorConfig(ctx context.Context, clusterConfig configv1.Network) (*uns.Unstructured, error) { | ||
operConfig := &netopv1.NetworkConfig{ | ||
TypeMeta: metav1.TypeMeta{APIVersion: "networkoperator.openshift.io/v1", Kind: "NetworkConfig"}, | ||
ObjectMeta: metav1.ObjectMeta{Name: names.OPERATOR_CONFIG}, | ||
} | ||
|
||
err := r.client.Get(ctx, types.NamespacedName{Name: names.OPERATOR_CONFIG}, operConfig) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
return nil, errors.Wrapf(err, "could not retrieve networkoperator.openshift.io/NetworkConfig %s", names.OPERATOR_CONFIG) | ||
} | ||
|
||
newOperConfig := operConfig.DeepCopy() | ||
network.MergeClusterConfig(&newOperConfig.Spec, clusterConfig.Spec) | ||
if reflect.DeepEqual(newOperConfig.Spec, operConfig.Spec) { | ||
return nil, nil | ||
} | ||
|
||
return k8sutil.ToUnstructured(newOperConfig) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I assume this name matches what other people are doing?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Once the installer switches from
NetworkConfig
toNetwork
, we can align the names if we like.