-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send bootstrap query from nodeup to kops-controller
- Loading branch information
1 parent
3f52eaa
commit 73041ab
Showing
9 changed files
with
274 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"k8s.io/kops/upup/pkg/fi" | ||
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks" | ||
) | ||
|
||
// SecretBuilder writes secrets | ||
type BootstrapClientBuilder struct { | ||
*NodeupModelContext | ||
} | ||
|
||
func (b BootstrapClientBuilder) Build(c *fi.ModelBuilderContext) error { | ||
if b.IsMaster || !b.UseKopsControllerForNodeBootstrap() { | ||
return nil | ||
} | ||
|
||
cert, err := b.GetCert(fi.CertificateIDCA) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bootstrapClient := &nodetasks.BootstrapClient{ | ||
CA: cert, | ||
} | ||
c.AddTask(bootstrapClient) | ||
return nil | ||
} | ||
|
||
var _ fi.ModelBuilder = &BootstrapClientBuilder{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nodeup | ||
|
||
const BootstrapAPIVersion = "bootstrap.kops.k8s.io/v1alpha1" | ||
|
||
// BootstrapRequest is a request from nodeup to kops-controller for bootstrapping a node. | ||
type BootstrapRequest struct { | ||
// APIVersion defines the versioned schema of this representation of a request. | ||
APIVersion string `json:"apiVersion"` | ||
} | ||
|
||
// BootstrapRespose is a response to a BootstrapRequest. | ||
type BootstrapResponse struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nodetasks | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
|
||
"k8s.io/kops/pkg/apis/nodeup" | ||
"k8s.io/kops/pkg/wellknownports" | ||
"k8s.io/kops/upup/pkg/fi" | ||
) | ||
|
||
type BootstrapClient struct { | ||
// CA is the CA certificate for kops-controller. | ||
CA []byte | ||
|
||
client *http.Client | ||
} | ||
|
||
var _ fi.Task = &BootstrapClient{} | ||
var _ fi.HasName = &BootstrapClient{} | ||
|
||
func (b *BootstrapClient) GetName() *string { | ||
name := "BootstrapClient" | ||
return &name | ||
} | ||
|
||
func (b *BootstrapClient) String() string { | ||
return "BootstrapClient" | ||
} | ||
|
||
func (b *BootstrapClient) Run(c *fi.Context) error { | ||
req := nodeup.BootstrapRequest{ | ||
APIVersion: nodeup.BootstrapAPIVersion, | ||
} | ||
|
||
err := b.queryBootstrap(c, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *BootstrapClient) queryBootstrap(c *fi.Context, req nodeup.BootstrapRequest) error { | ||
if b.client == nil { | ||
certPool := x509.NewCertPool() | ||
certPool.AppendCertsFromPEM(b.CA) | ||
|
||
b.client = &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
RootCAs: certPool, | ||
MinVersion: tls.VersionTLS12, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
reqBytes, err := json.Marshal(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bootstrapUrl := url.URL{ | ||
Scheme: "https", | ||
Host: net.JoinHostPort(c.Cluster.Spec.MasterInternalName, strconv.Itoa(wellknownports.KopsControllerPort)), | ||
Path: "/bootstrap", | ||
} | ||
resp, err := b.client.Post(bootstrapUrl.String(), "application/json", bytes.NewReader(reqBytes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
detail := "" | ||
if resp.Body != nil { | ||
scanner := bufio.NewScanner(resp.Body) | ||
if scanner.Scan() { | ||
detail = scanner.Text() | ||
} | ||
_ = resp.Body.Close() | ||
} | ||
return fmt.Errorf("bootstrap returned status code %d: %s", resp.StatusCode, detail) | ||
} | ||
|
||
var bootstrapResp nodeup.BootstrapResponse | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(body, &bootstrapResp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |