-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e6f310
commit 8b96e46
Showing
3 changed files
with
107 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package capi | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
controllerruntime "github.com/rancher/lasso/controller-runtime" | ||
"github.com/rancher/webhook/pkg/clients" | ||
"github.com/rancher/wrangler/pkg/schemes" | ||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3" | ||
clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4" | ||
addonsv1alpha3 "sigs.k8s.io/cluster-api/exp/addons/api/v1alpha3" | ||
expv1alpha3 "sigs.k8s.io/cluster-api/exp/api/v1alpha3" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func init() { | ||
_ = clientgoscheme.AddToScheme(schemes.All) | ||
_ = clusterv1alpha3.AddToScheme(schemes.All) | ||
_ = clusterv1alpha4.AddToScheme(schemes.All) | ||
_ = expv1alpha3.AddToScheme(schemes.All) | ||
_ = addonsv1alpha3.AddToScheme(schemes.All) | ||
_ = apiextensionsv1.AddToScheme(schemes.All) | ||
} | ||
|
||
const ( | ||
tlsCert = "/tmp/k8s-webhook-server/serving-certs/tls.crt" | ||
) | ||
|
||
func Register(ctx context.Context, clients *clients.Clients) (func(ctx context.Context) error, error) { | ||
mgr, err := ctrl.NewManager(clients.RESTConfig, ctrl.Options{ | ||
MetricsBindAddress: "0", | ||
NewCache: controllerruntime.NewNewCacheFunc(clients.SharedControllerFactory.SharedCacheFactory(), | ||
clients.Dynamic), | ||
Scheme: schemes.All, | ||
ClientDisableCacheFor: []client.Object{ | ||
&corev1.ConfigMap{}, | ||
&corev1.Secret{}, | ||
}, | ||
Port: 8777, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, webhook := range webhooks() { | ||
if err := webhook.SetupWebhookWithManager(mgr); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return func(ctx context.Context) error { | ||
if _, err := os.Stat(tlsCert); os.IsNotExist(err) { | ||
logrus.Errorf("Failed to file %s, not running capi webhooks", tlsCert) | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
return mgr.Start(ctx) | ||
}, nil | ||
} | ||
|
||
func webhooks() []webhook { | ||
return []webhook{ | ||
&clusterv1alpha4.Cluster{}, | ||
&clusterv1alpha4.Machine{}, | ||
&clusterv1alpha4.MachineHealthCheck{}, | ||
&clusterv1alpha4.MachineSet{}, | ||
&clusterv1alpha4.MachineDeployment{}, | ||
} | ||
} | ||
|
||
type webhook interface { | ||
SetupWebhookWithManager(mgr ctrl.Manager) error | ||
} |
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