-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put EnableInjectionOrDie back on the main path (#1772)
* put EnableInjectionOrDie back on the main path * nil check pointer type * move enable injeciton out of sharedmain * lint * nit picking fmt.... * add documentation * feedback cleanup * injection.GetRESTConfig * redirect code that moved:
Scott Nichols
authored
Oct 6, 2020
1 parent
8922d5c
commit 46761ba
Showing
4 changed files
with
170 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package injection | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"log" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
|
||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog" | ||
) | ||
|
||
// ParseAndGetRESTConfigOrDie parses the rest config flags and creates a client or | ||
// dies by calling log.Fatalf. | ||
func ParseAndGetRESTConfigOrDie() *rest.Config { | ||
var ( | ||
serverURL = flag.String("server", "", | ||
"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") | ||
kubeconfig = flag.String("kubeconfig", "", | ||
"Path to a kubeconfig. Only required if out-of-cluster.") | ||
) | ||
klog.InitFlags(flag.CommandLine) | ||
flag.Parse() | ||
|
||
cfg, err := GetRESTConfig(*serverURL, *kubeconfig) | ||
if err != nil { | ||
log.Fatal("Error building kubeconfig: ", err) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// GetRESTConfig returns a rest.Config to be used for kubernetes client creation. | ||
// It does so in the following order: | ||
// 1. Use the passed kubeconfig/serverURL. | ||
// 2. Fallback to the KUBECONFIG environment variable. | ||
// 3. Fallback to in-cluster config. | ||
// 4. Fallback to the ~/.kube/config. | ||
func GetRESTConfig(serverURL, kubeconfig string) (*rest.Config, error) { | ||
if kubeconfig == "" { | ||
kubeconfig = os.Getenv("KUBECONFIG") | ||
} | ||
|
||
// If we have an explicit indication of where the kubernetes config lives, read that. | ||
if kubeconfig != "" { | ||
c, err := clientcmd.BuildConfigFromFlags(serverURL, kubeconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
// If not, try the in-cluster config. | ||
if c, err := rest.InClusterConfig(); err == nil { | ||
return c, nil | ||
} | ||
|
||
// If no in-cluster config, try the default location in the user's home directory. | ||
if usr, err := user.Current(); err == nil { | ||
if c, err := clientcmd.BuildConfigFromFlags("", filepath.Join(usr.HomeDir, ".kube", "config")); err == nil { | ||
return c, nil | ||
} | ||
} | ||
|
||
return nil, errors.New("could not create a valid kubeconfig") | ||
} |
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,66 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package injection | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
"k8s.io/client-go/rest" | ||
|
||
"knative.dev/pkg/controller" | ||
"knative.dev/pkg/logging" | ||
"knative.dev/pkg/signals" | ||
) | ||
|
||
// EnableInjectionOrDie enables Knative Client Injection, and provides a | ||
// callback to start the informers. Both Context and Config are optional. | ||
// Returns context with rest config set and a callback to start the informers | ||
// after watches have been set. | ||
// | ||
// Typical integration: | ||
// ```go | ||
// ctx, startInformers := injection.EnableInjectionOrDie(signals.NewContext(), nil) | ||
// ... start watches with informers, if required ... | ||
// startInformers() | ||
// ``` | ||
func EnableInjectionOrDie(ctx context.Context, cfg *rest.Config) (context.Context, func()) { | ||
if ctx == nil { | ||
ctx = signals.NewContext() | ||
} | ||
if cfg == nil { | ||
cfg = ParseAndGetRESTConfigOrDie() | ||
} | ||
|
||
// Respect user provided settings, but if omitted customize the default behavior. | ||
if cfg.QPS == 0 { | ||
cfg.QPS = rest.DefaultQPS | ||
} | ||
if cfg.Burst == 0 { | ||
cfg.Burst = rest.DefaultBurst | ||
} | ||
ctx = WithConfig(ctx, cfg) | ||
|
||
ctx, informers := Default.SetupInformers(ctx, cfg) | ||
|
||
return ctx, func() { | ||
logging.FromContext(ctx).Info("Starting informers...") | ||
if err := controller.StartInformers(ctx.Done(), informers...); err != nil { | ||
logging.FromContext(ctx).Fatalw("Failed to start informers", zap.Error(err)) | ||
} | ||
} | ||
} |
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