Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exit nodeup gracefully if server already exists in k8s #15138

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.StatusNoContent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-picking, but we could also do http.StatusConflict - that's what I've seen from GCP APIs at least.

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 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 {
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