exit gracefully if server already exists in k8s

This commit is contained in:
Jesse Haka 2023-02-12 16:03:00 +02:00
parent 553270a06a
commit 8e6199fa39
4 changed files with 17 additions and 1 deletions

View File

@ -142,6 +142,12 @@ func (s *Server) bootstrap(w http.ResponseWriter, r *http.Request) {
id, err := s.verifier.VerifyToken(ctx, r, r.Header.Get("Authorization"), body, s.opt.Server.UseInstanceIDForNodeName) id, err := s.verifier.VerifyToken(ctx, r, r.Header.Get("Authorization"), body, s.opt.Server.UseInstanceIDForNodeName)
if err != nil { if err != nil {
// means that we should exit nodeup gracefully
if err == bootstrap.ErrAlreadyExists {
w.WriteHeader(http.StatusNoContent)
klog.Infof("%s: %v", r.RemoteAddr, err)
return
}
klog.Infof("bootstrap %s verify err: %v", r.RemoteAddr, err) klog.Infof("bootstrap %s verify err: %v", r.RemoteAddr, err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
// don't return the error; this allows us to have richer errors without security implications // don't return the error; this allows us to have richer errors without security implications

View File

@ -18,9 +18,12 @@ package bootstrap
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
) )
var ErrAlreadyExists = errors.New("node already exists")
// Authenticator generates authentication credentials for requests. // Authenticator generates authentication credentials for requests.
type Authenticator interface { type Authenticator interface {
CreateToken(body []byte) (string, error) CreateToken(body []byte) (string, error)

View File

@ -27,6 +27,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path" "path"
"time" "time"
@ -150,6 +151,12 @@ func (b *Client) Query(ctx context.Context, req any, resp any) error {
defer response.Body.Close() defer response.Body.Close()
} }
// if we receive StatusNoContent it means that we should exit gracefully
if response.StatusCode == http.StatusNoContent {
klog.Infof("kops-controller returned status code %d", response.StatusCode)
os.Exit(0)
}
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
detail := "" detail := ""
if response.Body != nil { if response.Body != nil {

View File

@ -154,7 +154,7 @@ func (o openstackVerifier) VerifyToken(ctx context.Context, rawRequest *http.Req
// check from kubernetes API does the instance already exist // check from kubernetes API does the instance already exist
_, err = o.kubeClient.CoreV1().Nodes().Get(ctx, instance.Name, v1.GetOptions{}) _, err = o.kubeClient.CoreV1().Nodes().Get(ctx, instance.Name, v1.GetOptions{})
if err == nil { if err == nil {
return nil, fmt.Errorf("server %q is already joined to kubernetes cluster", instance.Name) return nil, bootstrap.ErrAlreadyExists
} }
if err != nil && !errors.IsNotFound(err) { if err != nil && !errors.IsNotFound(err) {
return nil, fmt.Errorf("got error while querying kubernetes api: %w", err) return nil, fmt.Errorf("got error while querying kubernetes api: %w", err)