Skip to content

Commit

Permalink
Merge pull request #15138 from zetaab/exitgracefully
Browse files Browse the repository at this point in the history
exit nodeup gracefully if server already exists in k8s
  • Loading branch information
k8s-ci-robot committed Feb 20, 2023
2 parents 5f77223 + a765191 commit 511f32a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/kops-controller/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if err != nil {
// means that we should exit nodeup gracefully
if err == bootstrap.ErrAlreadyExists {
w.WriteHeader(http.StatusConflict)
klog.Infof("%s: %v", r.RemoteAddr, err)
return
}
klog.Infof("bootstrap %s verify err: %v", r.RemoteAddr, err)
w.WriteHeader(http.StatusForbidden)
// don't return the error; this allows us to have richer errors without security implications
Expand Down
3 changes: 3 additions & 0 deletions pkg/bootstrap/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package bootstrap

import (
"context"
"errors"
"net/http"
)

var ErrAlreadyExists = errors.New("node already exists")

// Authenticator generates authentication credentials for requests.
type Authenticator interface {
CreateToken(body []byte) (string, error)
Expand Down
7 changes: 7 additions & 0 deletions pkg/kopscontrollerclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"path"
"time"

Expand Down Expand Up @@ -150,6 +151,12 @@ func (b *Client) Query(ctx context.Context, req any, resp any) error {
defer response.Body.Close()
}

// if we receive StatusConflict it means that we should exit gracefully
if response.StatusCode == http.StatusConflict {
klog.Infof("kops-controller returned status code %d", response.StatusCode)
os.Exit(0)
}

if response.StatusCode != http.StatusOK {
detail := ""
if response.Body != nil {
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/openstack/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (o openstackVerifier) VerifyToken(ctx context.Context, rawRequest *http.Req
// check from kubernetes API does the instance already exist
_, err = o.kubeClient.CoreV1().Nodes().Get(ctx, instance.Name, v1.GetOptions{})
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) {
return nil, fmt.Errorf("got error while querying kubernetes api: %w", err)
Expand Down

0 comments on commit 511f32a

Please sign in to comment.