-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: server commands names (#212)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
8265e89
commit d8ad338
Showing
10 changed files
with
115 additions
and
95 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
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,101 @@ | ||
package authzserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/kyverno/kyverno-envoy-plugin/apis/v1alpha1" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/authz" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/policy" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/signals" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/multierr" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/tools/clientcmd" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
var httpAddress string | ||
var grpcAddress string | ||
var grpcNetwork string | ||
var kubeConfigOverrides clientcmd.ConfigOverrides | ||
command := &cobra.Command{ | ||
Use: "authz-server", | ||
Short: "Start the Kyverno Authz Server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// setup signals aware context | ||
return signals.Do(context.Background(), func(ctx context.Context) error { | ||
// track errors | ||
var httpErr, grpcErr, mgrErr error | ||
err := func(ctx context.Context) error { | ||
// create a rest config | ||
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
clientcmd.NewDefaultClientConfigLoadingRules(), | ||
&kubeConfigOverrides, | ||
) | ||
config, err := kubeConfig.ClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
// create a wait group | ||
var group wait.Group | ||
// wait all tasks in the group are over | ||
defer group.Wait() | ||
// create a controller manager | ||
scheme := runtime.NewScheme() | ||
if err := v1alpha1.Install(scheme); err != nil { | ||
return err | ||
} | ||
mgr, err := ctrl.NewManager(config, ctrl.Options{ | ||
Scheme: scheme, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to construct manager: %w", err) | ||
} | ||
// create compiler | ||
compiler := policy.NewCompiler() | ||
// create provider | ||
provider, err := policy.NewKubeProvider(mgr, compiler) | ||
if err != nil { | ||
return err | ||
} | ||
// create a cancellable context | ||
ctx, cancel := context.WithCancel(ctx) | ||
// start manager | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
mgrErr = mgr.Start(ctx) | ||
}) | ||
if !mgr.GetCache().WaitForCacheSync(ctx) { | ||
defer cancel() | ||
return fmt.Errorf("failed to wait for cache sync") | ||
} | ||
// create http and grpc servers | ||
http := authz.NewHttpServer(httpAddress) | ||
grpc := authz.NewGrpcServer(grpcNetwork, grpcAddress, provider) | ||
// run servers | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
httpErr = http.Run(ctx) | ||
}) | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
grpcErr = grpc.Run(ctx) | ||
}) | ||
return nil | ||
}(ctx) | ||
return multierr.Combine(err, httpErr, grpcErr, mgrErr) | ||
}) | ||
}, | ||
} | ||
command.Flags().StringVar(&httpAddress, "http-address", ":9080", "Address to listen on for health checks") | ||
command.Flags().StringVar(&grpcAddress, "grpc-address", ":9081", "Address to listen on") | ||
command.Flags().StringVar(&grpcNetwork, "grpc-network", "tcp", "Network to listen on") | ||
clientcmd.BindOverrideFlags(&kubeConfigOverrides, command.Flags(), clientcmd.RecommendedConfigOverrideFlags("kube-")) | ||
return command | ||
} |
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 |
---|---|---|
@@ -1,101 +1,17 @@ | ||
package serve | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/kyverno/kyverno-envoy-plugin/apis/v1alpha1" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/authz" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/policy" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/signals" | ||
authzserver "github.com/kyverno/kyverno-envoy-plugin/pkg/commands/serve/authz-server" | ||
sidecarinjector "github.com/kyverno/kyverno-envoy-plugin/pkg/commands/serve/sidecar-injector" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/multierr" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/tools/clientcmd" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
var httpAddress string | ||
var grpcAddress string | ||
var grpcNetwork string | ||
var kubeConfigOverrides clientcmd.ConfigOverrides | ||
command := &cobra.Command{ | ||
Use: "serve", | ||
Short: "Start the kyverno-envoy-plugin server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// setup signals aware context | ||
return signals.Do(context.Background(), func(ctx context.Context) error { | ||
// track errors | ||
var httpErr, grpcErr, mgrErr error | ||
err := func(ctx context.Context) error { | ||
// create a rest config | ||
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
clientcmd.NewDefaultClientConfigLoadingRules(), | ||
&kubeConfigOverrides, | ||
) | ||
config, err := kubeConfig.ClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
// create a wait group | ||
var group wait.Group | ||
// wait all tasks in the group are over | ||
defer group.Wait() | ||
// create a controller manager | ||
scheme := runtime.NewScheme() | ||
if err := v1alpha1.Install(scheme); err != nil { | ||
return err | ||
} | ||
mgr, err := ctrl.NewManager(config, ctrl.Options{ | ||
Scheme: scheme, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to construct manager: %w", err) | ||
} | ||
// create compiler | ||
compiler := policy.NewCompiler() | ||
// create provider | ||
provider, err := policy.NewKubeProvider(mgr, compiler) | ||
if err != nil { | ||
return err | ||
} | ||
// create a cancellable context | ||
ctx, cancel := context.WithCancel(ctx) | ||
// start manager | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
mgrErr = mgr.Start(ctx) | ||
}) | ||
if !mgr.GetCache().WaitForCacheSync(ctx) { | ||
defer cancel() | ||
return fmt.Errorf("failed to wait for cache sync") | ||
} | ||
// create http and grpc servers | ||
http := authz.NewHttpServer(httpAddress) | ||
grpc := authz.NewGrpcServer(grpcNetwork, grpcAddress, provider) | ||
// run servers | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
httpErr = http.Run(ctx) | ||
}) | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// cancel context at the end | ||
defer cancel() | ||
grpcErr = grpc.Run(ctx) | ||
}) | ||
return nil | ||
}(ctx) | ||
return multierr.Combine(err, httpErr, grpcErr, mgrErr) | ||
}) | ||
}, | ||
Short: "Run Kyverno Envoy Plugin servers", | ||
} | ||
command.Flags().StringVar(&httpAddress, "http-address", ":9080", "Address to listen on for health checks") | ||
command.Flags().StringVar(&grpcAddress, "grpc-address", ":9081", "Address to listen on") | ||
command.Flags().StringVar(&grpcNetwork, "grpc-network", "tcp", "Network to listen on") | ||
clientcmd.BindOverrideFlags(&kubeConfigOverrides, command.Flags(), clientcmd.RecommendedConfigOverrideFlags("kube-")) | ||
command.AddCommand(authzserver.Command()) | ||
command.AddCommand(sidecarinjector.Command()) | ||
return command | ||
} |
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