github.com/annwntech/go-micro/v2@v2.9.5/util/kubernetes/client/util.go (about) 1 package client 2 3 import ( 4 "crypto/x509" 5 "encoding/pem" 6 "errors" 7 "fmt" 8 "io" 9 "io/ioutil" 10 "strings" 11 "text/template" 12 ) 13 14 // renderTemplateFile renders template for a given resource into writer w 15 func renderTemplate(resource string, w io.Writer, data interface{}) error { 16 t := template.Must(template.New("kubernetes").Parse(templates[resource])) 17 18 if err := t.Execute(w, data); err != nil { 19 return err 20 } 21 22 return nil 23 } 24 25 // COPIED FROM 26 // https://github.com/kubernetes/kubernetes/blob/7a725418af4661067b56506faabc2d44c6d7703a/pkg/util/crypto/crypto.go 27 28 // CertPoolFromFile returns an x509.CertPool containing the certificates in the given PEM-encoded file. 29 // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates 30 func CertPoolFromFile(filename string) (*x509.CertPool, error) { 31 certs, err := certificatesFromFile(filename) 32 if err != nil { 33 return nil, err 34 } 35 pool := x509.NewCertPool() 36 for _, cert := range certs { 37 pool.AddCert(cert) 38 } 39 return pool, nil 40 } 41 42 // certificatesFromFile returns the x509.Certificates contained in the given PEM-encoded file. 43 // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates 44 func certificatesFromFile(file string) ([]*x509.Certificate, error) { 45 if len(file) == 0 { 46 return nil, errors.New("error reading certificates from an empty filename") 47 } 48 pemBlock, err := ioutil.ReadFile(file) 49 if err != nil { 50 return nil, err 51 } 52 certs, err := CertsFromPEM(pemBlock) 53 if err != nil { 54 return nil, fmt.Errorf("error reading %s: %s", file, err) 55 } 56 return certs, nil 57 } 58 59 // CertsFromPEM returns the x509.Certificates contained in the given PEM-encoded byte array 60 // Returns an error if a certificate could not be parsed, or if the data does not contain any certificates 61 func CertsFromPEM(pemCerts []byte) ([]*x509.Certificate, error) { 62 ok := false 63 certs := []*x509.Certificate{} 64 for len(pemCerts) > 0 { 65 var block *pem.Block 66 block, pemCerts = pem.Decode(pemCerts) 67 if block == nil { 68 break 69 } 70 // Only use PEM "CERTIFICATE" blocks without extra headers 71 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { 72 continue 73 } 74 75 cert, err := x509.ParseCertificate(block.Bytes) 76 if err != nil { 77 return certs, err 78 } 79 80 certs = append(certs, cert) 81 ok = true 82 } 83 84 if !ok { 85 return certs, errors.New("could not read any certificates") 86 } 87 return certs, nil 88 } 89 90 // Format is used to format a string value into a k8s valid name 91 func Format(v string) string { 92 // to lower case 93 v = strings.ToLower(v) 94 // / to dashes 95 v = strings.ReplaceAll(v, "/", "-") 96 // dots to dashes 97 v = strings.ReplaceAll(v, ".", "-") 98 // limit to 253 chars 99 if len(v) > 253 { 100 v = v[:253] 101 } 102 // return new name 103 return v 104 }