generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 293
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: tenzen-y <[email protected]>
- Loading branch information
Showing
11 changed files
with
350 additions
and
98 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
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,93 @@ | ||
package jobframework | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
|
||
"sigs.k8s.io/kueue/pkg/controller/jobs/noop" | ||
"sigs.k8s.io/kueue/pkg/util/cert" | ||
) | ||
|
||
type kubeVersionFunc func(fwkName string, opts ...Option) ([]Option, error) | ||
|
||
var ( | ||
errFailedMappingResource = errors.New("restMapper failed mapping resource") | ||
) | ||
|
||
func SetupControllers( | ||
mgr ctrl.Manager, | ||
setupLog logr.Logger, | ||
kubeVersionFunc kubeVersionFunc, | ||
certsReady chan struct{}, | ||
opts ...Option, | ||
) error { | ||
// The controllers won't work until the webhooks are operating, and the webhook won't work until the | ||
// certs are all in place. | ||
cert.WaitForCertsReady(setupLog, certsReady) | ||
options := DefaultOptions | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
|
||
return ForEachIntegration(func(name string, cb IntegrationCallbacks) error { | ||
log := setupLog.WithValues("jobFrameworkName", name) | ||
fwkNamePrefix := fmt.Sprintf("jobFrameworkName %q", name) | ||
|
||
if options.EnabledFrameworks.Has(name) { | ||
gvk, err := apiutil.GVKForObject(cb.JobType, mgr.GetScheme()) | ||
if err != nil { | ||
return fmt.Errorf("%s: %w: %w", fwkNamePrefix, errFailedMappingResource, err) | ||
} | ||
if _, err = mgr.GetRESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version); err != nil { | ||
if !meta.IsNoMatchError(err) { | ||
return fmt.Errorf("%s: %w", fwkNamePrefix, err) | ||
} | ||
log.Info("No matching API in the server for job framework, skipped setup of controller and webhook") | ||
} else { | ||
if err = cb.NewReconciler( | ||
mgr.GetClient(), | ||
mgr.GetEventRecorderFor(fmt.Sprintf("%s-%s-controller", name, options.ManagerName)), | ||
opts..., | ||
).SetupWithManager(mgr); err != nil { | ||
return fmt.Errorf("%s: %w", fwkNamePrefix, err) | ||
} | ||
if kubeVersionFunc != nil { | ||
if opts, err = kubeVersionFunc(name, opts...); err != nil { | ||
return fmt.Errorf("%s: failed to configure reconcilers: %w", fwkNamePrefix, err) | ||
} | ||
} | ||
if err = cb.SetupWebhook(mgr, opts...); err != nil { | ||
return fmt.Errorf("%s: unable to create webhook: %w", fwkNamePrefix, err) | ||
} | ||
log.Info("Set up controller and webhook for job framework") | ||
return nil | ||
} | ||
} | ||
if err := noop.SetupWebhook(mgr, cb.JobType); err != nil { | ||
return fmt.Errorf("%s: unable to create noop webhook: %w", fwkNamePrefix, err) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func SetupIndexes(ctx context.Context, indexer client.FieldIndexer, opts ...Option) error { | ||
options := DefaultOptions | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
return ForEachIntegration(func(name string, cb IntegrationCallbacks) error { | ||
if options.EnabledFrameworks.Has(name) { | ||
if err := cb.SetupIndexes(ctx, indexer); err != nil { | ||
return fmt.Errorf("jobFrameworkName %q: %w", name, err) | ||
} | ||
} | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.