github.com/oam-dev/cluster-gateway@v1.9.0/cmd/apiserver/main.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7      http://www.apache.org/licenses/LICENSE-2.0
     8  Unless required by applicable law or agreed to in writing, software
     9  distributed under the License is distributed on an "AS IS" BASIS,
    10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  See the License for the specific language governing permissions and
    12  limitations under the License.
    13  */
    14  
    15  package main
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"k8s.io/apimachinery/pkg/util/sets"
    21  	"k8s.io/apiserver/pkg/endpoints/request"
    22  	"k8s.io/apiserver/pkg/server"
    23  	"k8s.io/klog/v2"
    24  	"sigs.k8s.io/apiserver-runtime/pkg/builder"
    25  
    26  	genericfilters "k8s.io/apiserver/pkg/server/filters"
    27  
    28  	"github.com/oam-dev/cluster-gateway/pkg/config"
    29  	"github.com/oam-dev/cluster-gateway/pkg/metrics"
    30  	"github.com/oam-dev/cluster-gateway/pkg/options"
    31  	"github.com/oam-dev/cluster-gateway/pkg/util/singleton"
    32  
    33  	// +kubebuilder:scaffold:resource-imports
    34  	clusterv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
    35  
    36  	_ "github.com/oam-dev/cluster-gateway/pkg/featuregates"
    37  )
    38  
    39  func main() {
    40  
    41  	// registering metrics
    42  	metrics.Register()
    43  
    44  	cmd, err := builder.APIServer.
    45  		// +kubebuilder:scaffold:resource-register
    46  		WithResource(&clusterv1alpha1.ClusterGateway{}).
    47  		WithResource(&clusterv1alpha1.VirtualCluster{}).
    48  		WithLocalDebugExtension().
    49  		ExposeLoopbackMasterClientConfig().
    50  		ExposeLoopbackAuthorizer().
    51  		WithoutEtcd().
    52  		WithConfigFns(func(config *server.RecommendedConfig) *server.RecommendedConfig {
    53  			config.LongRunningFunc = func(r *http.Request, requestInfo *request.RequestInfo) bool {
    54  				if requestInfo.Resource == "clustergateways" && requestInfo.Subresource == "proxy" {
    55  					return true
    56  				}
    57  				return genericfilters.BasicLongRunningRequestCheck(sets.NewString("watch"), sets.NewString())(r, requestInfo)
    58  			}
    59  			return config
    60  		}, config.WithUserAgent).
    61  		WithOptionsFns(func(options *builder.ServerOptions) *builder.ServerOptions {
    62  			if err := config.ValidateSecret(); err != nil {
    63  				klog.Fatal(err)
    64  			}
    65  			if err := config.ValidateClusterProxy(); err != nil {
    66  				klog.Fatal(err)
    67  			}
    68  			if err := clusterv1alpha1.LoadGlobalClusterGatewayProxyConfig(); err != nil {
    69  				klog.Fatal(err)
    70  			}
    71  			return options
    72  		}).
    73  		WithServerFns(func(server *builder.GenericAPIServer) *builder.GenericAPIServer {
    74  			server.Handler.FullHandlerChain = clusterv1alpha1.NewClusterGatewayProxyRequestEscaper(server.Handler.FullHandlerChain)
    75  			return server
    76  		}).
    77  		WithPostStartHook("init-master-loopback-client", singleton.InitLoopbackClient).
    78  		Build()
    79  	if err != nil {
    80  		klog.Fatal(err)
    81  	}
    82  	config.AddLogFlags(cmd.Flags())
    83  	config.AddSecretFlags(cmd.Flags())
    84  	config.AddVirtualClusterFlags(cmd.Flags())
    85  	config.AddClusterProxyFlags(cmd.Flags())
    86  	config.AddProxyAuthorizationFlags(cmd.Flags())
    87  	config.AddUserAgentFlags(cmd.Flags())
    88  	config.AddClusterGatewayProxyConfig(cmd.Flags())
    89  	cmd.Flags().BoolVarP(&options.OCMIntegration, "ocm-integration", "", false,
    90  		"Enabling OCM integration, reading cluster CA and api endpoint from managed "+
    91  			"cluster.")
    92  	if err := cmd.Execute(); err != nil {
    93  		klog.Fatal(err)
    94  	}
    95  }