97c1300f53
* Add Get() and GetOptions. * Removed watcher. Outline of client. YAML templates * Added default service and deployment templates and types * Added API tests and cleaned up errors. * Small refactoring. Template package is no more. * Ripped out existing code in preparation to small rework * Reshuffled the source code to make it organized better * Create service and deployment in kubernetes runtime * Major cleanup and refactoring of Kubernetes runtime * Service now handles low level K8s API calls across both K8s deployment an service API objects * Runtime has a task queue that serves for queueing runtime action requests * General refactoring * No need for Lock in k8s service * Added kubernetes runtime env var to default deployment * Enable running different versions of the same service * Can't delete services through labels * Proto cruft. Added runtime.CreateOptions implementation in proto * Removed proxy service from default env variables * Make service name mandatory param to Get method * Get Delete changes from https://github.com/micro/go-micro/pull/945 * Replaced template files with global variables * Validate service names before sending K8s API request * Refactored Kubernetes API client. Fixed typos. * Added client.Resource to make API resources more explicit in code
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package client
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"text/template"
|
|
)
|
|
|
|
// renderTemplateFile renders template file in path into writer w with supplied data
|
|
func renderTemplate(text string, w io.Writer, data interface{}) error {
|
|
t := template.Must(template.New("kubernetes").Parse(text))
|
|
|
|
if err := t.Execute(w, data); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// COPIED FROM
|
|
// https://github.com/kubernetes/kubernetes/blob/7a725418af4661067b56506faabc2d44c6d7703a/pkg/util/crypto/crypto.go
|
|
|
|
// CertPoolFromFile returns an x509.CertPool containing the certificates in the given PEM-encoded file.
|
|
// 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
|
|
func CertPoolFromFile(filename string) (*x509.CertPool, error) {
|
|
certs, err := certificatesFromFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pool := x509.NewCertPool()
|
|
for _, cert := range certs {
|
|
pool.AddCert(cert)
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
// certificatesFromFile returns the x509.Certificates contained in the given PEM-encoded file.
|
|
// 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
|
|
func certificatesFromFile(file string) ([]*x509.Certificate, error) {
|
|
if len(file) == 0 {
|
|
return nil, errors.New("error reading certificates from an empty filename")
|
|
}
|
|
pemBlock, err := ioutil.ReadFile(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
certs, err := CertsFromPEM(pemBlock)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading %s: %s", file, err)
|
|
}
|
|
return certs, nil
|
|
}
|
|
|
|
// CertsFromPEM returns the x509.Certificates contained in the given PEM-encoded byte array
|
|
// Returns an error if a certificate could not be parsed, or if the data does not contain any certificates
|
|
func CertsFromPEM(pemCerts []byte) ([]*x509.Certificate, error) {
|
|
ok := false
|
|
certs := []*x509.Certificate{}
|
|
for len(pemCerts) > 0 {
|
|
var block *pem.Block
|
|
block, pemCerts = pem.Decode(pemCerts)
|
|
if block == nil {
|
|
break
|
|
}
|
|
// Only use PEM "CERTIFICATE" blocks without extra headers
|
|
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
|
|
continue
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
if err != nil {
|
|
return certs, err
|
|
}
|
|
|
|
certs = append(certs, cert)
|
|
ok = true
|
|
}
|
|
|
|
if !ok {
|
|
return certs, errors.New("could not read any certificates")
|
|
}
|
|
return certs, nil
|
|
}
|