kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.99k stars 2.93k forks source link

How to specify the cipherSuites for kubernetes.ClientSet? #1357

Closed cxisama closed 3 months ago

cxisama commented 3 months ago

First of all, I Customize a rest.Config and configure WrapTransport for it. After obtaining rest.config, use the config.wrap method to configure CipherSuites for transport.The code is as follows:

type customCipherRoundTripper struct {
    customRt http.RoundTripper
}
func (t *customCipherRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    // Force conversion customRt to http.Transport
    tlsTran, ok := t.customRt.(*http.Transport)
    if ok {
        tlsTran.TLSClientConfig.CipherSuites = []uint16{}
    }
    return t.customRt.RoundTrip(req)
}

func InitClient(){
    rest.AddUserAgent(conf, userAgent)
    // create a new clientSet for config, but this config does not have CipherSuites
    clientSet, err = kubernetes.NewForConfig(conf)
}

The clientSet does not use the specified CipherSuites when accessing Kubernetes as a client. Instead, the native CipherSuite of Kubernetes is used. How do I specify the default CipherSuites for clientSet?

light0011 commented 2 months ago
type customCipherRoundTripper struct {
    originalTransport http.RoundTripper
}

func (t *customCipherRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    // Force conversion customRt to http.Transport
    tlsTran, ok := t.originalTransport.(*http.Transport)
    if ok {
        tlsTran.TLSClientConfig.CipherSuites = []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384}
    }
    return t.originalTransport.RoundTrip(req)
}

func createClientSets(opt *options.TrainingJobOperatorOption) (kubeclientset.Interface, kubeclientset.Interface, trainingjobclientset.Interface, apiextensionsclient.Interface, *vcclient.Clientset, error) {
    var kubeConfig *restclientset.Config
    var err error

    if opt.RunInCluster {
        kubeConfig, err = restclientset.InClusterConfig()
    } else {
        if opt.Kubeconfig != "" {

            if _, err = os.Stat(opt.Kubeconfig); err != nil {
                opt.Kubeconfig = ""
            }
            klog.V(4).Infof("Using kubeconfig file: %s", opt.Kubeconfig)
        }
        kubeConfig, err = clientcmd.BuildConfigFromFlags(opt.MasterUrl, opt.Kubeconfig)
    }
    kubeConfig.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
        return &customCipherRoundTripper{rt}
    }

}   

you can try these codes