-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move enable injeciton out of sharedmain
- Loading branch information
Scott Nichols
committed
Oct 6, 2020
1 parent
11f5e2d
commit 7dfb20f
Showing
5 changed files
with
157 additions
and
45 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,88 @@ | ||
/* | ||
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" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog" | ||
"log" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
) | ||
|
||
// 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.Fatalf("Error building kubeconfig: %v", err) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// GetConfig 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") | ||
} | ||
|
||
// We produce configs a bunch of ways, this gives us a single place | ||
// to "decorate" them with common useful things (e.g. for debugging) | ||
decorate := func(cfg *rest.Config) *rest.Config { | ||
return cfg | ||
} | ||
|
||
// 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 decorate(c), nil | ||
} | ||
// If not, try the in-cluster config. | ||
if c, err := rest.InClusterConfig(); err == nil { | ||
return decorate(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 decorate(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 |
---|---|---|
|
@@ -18,7 +18,6 @@ package injection | |
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/client-go/rest" | ||
) | ||
|
||
|
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,57 @@ | ||
/* | ||
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" | ||
"knative.dev/pkg/controller" | ||
"knative.dev/pkg/logging" | ||
"knative.dev/pkg/signals" | ||
|
||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// EnableInjectionOrDie enables Knative Injection and starts the informers. | ||
// Both Context and Config are optional. Returns context with rest config set | ||
// and a function to start the informers after watches have been set up. | ||
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