-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xuezhaojun <[email protected]>
- Loading branch information
1 parent
fcc928c
commit 842192a
Showing
9 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package flightctl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/openshift/library-go/pkg/operator/events" | ||
) | ||
|
||
type FlightCtlManager struct { | ||
events.Recorder | ||
enabled bool | ||
} | ||
|
||
func NewFlightCtlManager(recorder events.Recorder) *FlightCtlManager { | ||
return &FlightCtlManager{ | ||
enabled: false, | ||
Recorder: recorder, | ||
} | ||
} | ||
|
||
func (m *FlightCtlManager) SetEnabled(enabled bool) { | ||
m.Eventf("FlightCtlManagerEnabled", "flightctl is %s", func() string { | ||
if enabled { | ||
return "enabled" | ||
} | ||
return "disabled" | ||
}) | ||
m.enabled = enabled | ||
} | ||
|
||
func (m *FlightCtlManager) IsEnabled() bool { | ||
return m.enabled | ||
} | ||
|
||
func (m *FlightCtlManager) IsManagedClusterADevice(ctx context.Context, managedClusterName string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
func (m *FlightCtlManager) IsDeviceApproved(ctx context.Context, deviceID string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
func (m *FlightCtlManager) GetRepository(ctx context.Context) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func (m *FlightCtlManager) ApplyRepository(ctx context.Context) error { | ||
return nil | ||
} | ||
|
||
func (m *FlightCtlManager) RemoveRepository(ctx context.Context) error { | ||
return 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 flightctl | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/stolostron/managedcluster-import-controller/pkg/helpers" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
const ControllerName = "flightctl-controller" | ||
const FlightCtlNamespace = "flightctl" | ||
|
||
func Add(ctx context.Context, mgr manager.Manager, clientHolder *helpers.ClientHolder) error { | ||
return ctrl.NewControllerManagedBy(mgr).Named(ControllerName). | ||
Watches( | ||
&corev1.Namespace{}, | ||
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request { | ||
return []reconcile.Request{ | ||
{ | ||
NamespacedName: types.NamespacedName{Name: o.GetName()}, | ||
}, | ||
} | ||
}), | ||
builder.WithPredicates(predicate.Funcs{ | ||
GenericFunc: func(e event.GenericEvent) bool { return e.Object.GetName() == FlightCtlNamespace }, | ||
CreateFunc: func(e event.CreateEvent) bool { return e.Object.GetName() == FlightCtlNamespace }, | ||
DeleteFunc: func(e event.DeleteEvent) bool { return e.Object.GetName() == FlightCtlNamespace }, | ||
UpdateFunc: func(e event.UpdateEvent) bool { return e.ObjectNew.GetName() == FlightCtlNamespace }, | ||
}), | ||
). | ||
Complete(&NSController{ | ||
clientHolder: clientHolder, | ||
scheme: mgr.GetScheme(), | ||
recorder: helpers.NewEventRecorder(clientHolder.KubeClient, ControllerName), | ||
}) | ||
} |
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,12 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: flightctl-client-agent-registration | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: managedcluster-import-controller-agent-regitration-client | ||
subjects: | ||
- kind: ServiceAccount | ||
name: flightctl-client | ||
namespace: "{{ .Namespace }}" |
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,4 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: flightctl-client |
7 changes: 7 additions & 0 deletions
7
pkg/controller/flightctl/manifests/serviceaccounttokensecret.yml
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,7 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: flightctl-client-token | ||
annotations: | ||
kubernetes.io/service-account.name: flightctl-client | ||
type: kubernetes.io/service-account-token |
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,70 @@ | ||
package flightctl | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
|
||
"github.com/openshift/library-go/pkg/operator/events" | ||
"github.com/stolostron/managedcluster-import-controller/pkg/helpers" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
var log = logf.Log.WithName(ControllerName) | ||
|
||
//go:embed manifests | ||
var FlightCtlManifestFiles embed.FS | ||
|
||
var files = []string{ | ||
"manifests/clusterrolebinding.yml", | ||
"manifests/serviceaccount.yml", | ||
"manifests/serviceaccounttokensecret.yml", | ||
} | ||
|
||
var _ reconcile.Reconciler = &NSController{} | ||
|
||
// NS controller is responsible for creating FlightCtl resources when namespace `flightctl` is created. | ||
type NSController struct { | ||
clientHolder *helpers.ClientHolder | ||
recorder events.Recorder | ||
scheme *runtime.Scheme | ||
} | ||
|
||
func (c *NSController) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
log.Info("Reconciling FlightCtl namespace", "namespace", request.Name) | ||
var err error | ||
|
||
// get the FlightCtl namespace | ||
// if ns found, create resources and set owner reference to the ns. | ||
ns := &corev1.Namespace{} | ||
err = c.clientHolder.RuntimeClient.Get(ctx, request.NamespacedName, ns) | ||
if errors.IsNotFound(err) { | ||
// if ns not found, delete the Repository we created | ||
return reconcile.Result{}, nil | ||
} | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// create rbac resources and set owner reference to the ns. | ||
objects, err := helpers.FilesToObjects(files, struct { | ||
Namespace string | ||
}{ | ||
Namespace: ns.Name, | ||
}, &FlightCtlManifestFiles) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
if _, err := helpers.ApplyResources( | ||
c.clientHolder, c.recorder, c.scheme, ns, objects...); err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// TODO: create Repository resources | ||
// TODO: create auto-approver of flightctl | ||
|
||
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