remove unused functions and simplify return str

This commit is contained in:
tanjunchen 2019-10-15 23:54:10 +08:00
parent d84e761cf3
commit fa6d0130c8
11 changed files with 8 additions and 315 deletions

View File

@ -76,7 +76,7 @@ func (b *CloudConfigBuilder) Build(c *fi.ModelBuilderContext) error {
lines = append(lines, "ElbSecurityGroup = "+*cloudConfig.ElbSecurityGroup)
}
case "vsphere":
vm_uuid, err := getVMUUID(b.Cluster.Spec.KubernetesVersion)
vmUUID, err := getVMUUID(b.Cluster.Spec.KubernetesVersion)
if err != nil {
return err
}
@ -99,8 +99,8 @@ func (b *CloudConfigBuilder) Build(c *fi.ModelBuilderContext) error {
if cloudConfig.VSphereDatastore != nil {
lines = append(lines, "datastore = "+*cloudConfig.VSphereDatastore)
}
if vm_uuid != "" {
lines = append(lines, "vm-uuid = "+strings.Trim(vm_uuid, "\n"))
if vmUUID != "" {
lines = append(lines, "vm-uuid = "+strings.Trim(vmUUID, "\n"))
}
// Disk Config for vSphere CloudProvider
// We need this to support Kubernetes vSphere CloudProvider < v1.5.3
@ -198,11 +198,11 @@ func getVMUUID(kubernetesVersion string) (string, error) {
defer try.CloseFile(file)
vm_uuid, err := bufio.NewReader(file).ReadString('\n')
vmUUID, err := bufio.NewReader(file).ReadString('\n')
if err != nil {
return "", err
}
return vm_uuid, err
return vmUUID, err
}
return "", err

View File

@ -25,12 +25,6 @@ import (
"k8s.io/kops/pkg/assets"
)
type ClusterParams struct {
CloudProvider string
KubernetesVersion string
UpdatePolicy string
}
func buildCluster() *api.Cluster {
return &api.Cluster{

View File

@ -9,7 +9,6 @@ go_library(
importpath = "k8s.io/kops/pkg/resources/gce",
visibility = ["//visibility:public"],
deps = [
"//dnsprovider/pkg/dnsprovider:go_default_library",
"//pkg/resources:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/cloudup/gce:go_default_library",

View File

@ -24,7 +24,6 @@ import (
compute "google.golang.org/api/compute/v0.beta"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
@ -819,77 +818,3 @@ func (d *clusterDiscoveryGCE) listGCEDNSZone() ([]*resources.Resource, error) {
// },
//}, nil
}
func (d *clusterDiscoveryGCE) findDNSZone() (dnsprovider.Zone, error) {
dnsProvider, err := d.cloud.DNS()
if err != nil {
return nil, fmt.Errorf("Error getting dnsprovider: %v", err)
}
zonesLister, supported := dnsProvider.Zones()
if !supported {
return nil, fmt.Errorf("DNS provier does not support listing zones: %v", err)
}
allZones, err := zonesLister.List()
if err != nil {
return nil, fmt.Errorf("Error listing dns zones: %v", err)
}
for _, zone := range allZones {
if strings.Contains(d.clusterName, strings.TrimSuffix(zone.Name(), ".")) {
return zone, nil
}
}
return nil, fmt.Errorf("DNS Zone for cluster %s could not be found", d.clusterName)
}
func (d *clusterDiscoveryGCE) deleteDNSZone(cloud fi.Cloud, r *resources.Resource) error {
clusterZone := r.Obj.(dnsprovider.Zone)
rrs, supported := clusterZone.ResourceRecordSets()
if !supported {
return fmt.Errorf("ResourceRecordSets not supported with clouddns")
}
records, err := rrs.List()
if err != nil {
return fmt.Errorf("Failed to list resource records")
}
changeset := rrs.StartChangeset()
for _, record := range records {
if record.Type() != "A" {
continue
}
name := record.Name()
name = "." + strings.TrimSuffix(name, ".")
prefix := strings.TrimSuffix(name, "."+d.clusterName)
remove := false
// TODO: Compute the actual set of names?
if prefix == ".api" || prefix == ".api.internal" {
remove = true
} else if strings.HasPrefix(prefix, ".etcd-") {
remove = true
}
if !remove {
continue
}
changeset.Remove(record)
}
if changeset.IsEmpty() {
return nil
}
err = changeset.Apply()
if err != nil {
return fmt.Errorf("Error deleting cloud dns records: %v", err)
}
return nil
}

View File

@ -79,20 +79,6 @@ func (s *state) Merge(b []byte) error {
return nil
}
func (s *state) get(key string) []byte {
s.mtx.RLock()
defer s.mtx.RUnlock()
v, found := s.data.Records[key]
if !found {
return nil
}
if v.Tombstone {
return nil
}
return v.Data
}
func (s *state) now() uint64 {
// TODO: This relies on NTP. We could have a g-counter or something, but this is probably good enough for V1
// It's good enough for weave :-)
@ -124,25 +110,6 @@ func (s *state) snapshot() *gossip.GossipStateSnapshot {
return snapshot
}
func (s *state) put(key string, data []byte) {
s.mtx.Lock()
defer s.mtx.Unlock()
now := s.now()
v := &mesh.KVStateRecord{
Data: data,
Version: now,
}
if s.data.Records == nil {
s.data.Records = make(map[string]*mesh.KVStateRecord)
}
s.data.Records[key] = v
s.version++
}
func (s *state) updateValues(removeKeys []string, putEntries map[string]string) {
if len(removeKeys) == 0 && len(putEntries) == 0 {
return

View File

@ -87,10 +87,6 @@ func (p *peer) updateValues(removeKeys []string, putEntries map[string]string) e
return nil
}
func (p *peer) stop() {
close(p.quit)
}
// Return a copy of our complete state.
func (p *peer) Gossip() (complete mesh.GossipData) {
data := p.st.getData()

View File

@ -48,20 +48,6 @@ func newState(self mesh.PeerName) *state {
}
}
func (s *state) get(key string) []byte {
s.mtx.RLock()
defer s.mtx.RUnlock()
v, found := s.data.Records[key]
if !found {
return nil
}
if v.Tombstone {
return nil
}
return v.Data
}
func (s *state) now() uint64 {
// TODO: This relies on NTP. We could have a g-counter or something, but this is probably good enough for V1
// It's good enough for weave :-)
@ -93,25 +79,6 @@ func (s *state) snapshot() *gossip.GossipStateSnapshot {
return snapshot
}
func (s *state) put(key string, data []byte) {
s.mtx.Lock()
defer s.mtx.Unlock()
now := s.now()
v := &KVStateRecord{
Data: data,
Version: now,
}
if s.data.Records == nil {
s.data.Records = make(map[string]*KVStateRecord)
}
s.data.Records[key] = v
s.version++
}
func (s *state) updateValues(removeKeys []string, putEntries map[string]string) {
if len(removeKeys) == 0 && len(putEntries) == 0 {
return

View File

@ -17,7 +17,6 @@ limitations under the License.
package assettasks
import (
"bufio"
"fmt"
"github.com/docker/engine-api/client"
@ -79,62 +78,6 @@ func (d *dockerAPI) findImage(name string) (*types.Image, error) {
return nil, nil
}
// pullImage does `docker pull`, via the API.
// Because it is non-trivial to get credentials, we tend to use the CLI
func (d *dockerAPI) pullImage(name string) error {
klog.V(4).Infof("docker pull for image %q", name)
ctx := context.Background()
pullOptions := types.ImagePullOptions{}
resp, err := d.client.ImagePull(ctx, name, pullOptions)
if resp != nil {
defer resp.Close()
}
if err != nil {
return fmt.Errorf("error pulling image %q: %v", name, err)
}
scanner := bufio.NewScanner(resp)
for scanner.Scan() {
// {"status":"Already exists","progressDetail":{},"id":"a3ed95caeb02"}
// {"status":"Status: Image is up to date for k8s.gcr.io/cluster-proportional-autoscaler-amd64:1.0.0"}
klog.Infof("docker pull %s", scanner.Text())
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error pulling image %q: %v", name, err)
}
return nil
}
// pushImage does `docker push`, via the API.
// Because it is non-trivial to get credentials, we tend to use the CLI
func (d *dockerAPI) pushImage(name string) error {
klog.V(4).Infof("docker push for image %q", name)
ctx := context.Background()
options := types.ImagePushOptions{}
resp, err := d.client.ImagePush(ctx, name, options)
if resp != nil {
defer resp.Close()
}
if err != nil {
return fmt.Errorf("error pushing image %q: %v", name, err)
}
scanner := bufio.NewScanner(resp)
for scanner.Scan() {
klog.Infof("docker pushing %s", scanner.Text())
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error pushing image %q: %v", name, err)
}
return nil
}
// tagImage does a `docker tag`, via the API
func (d *dockerAPI) tagImage(imageID string, ref string) error {
klog.V(4).Infof("docker tag for image %q, tag %q", imageID, ref)

View File

@ -18,8 +18,6 @@ package fi
import (
"bytes"
crypto_rand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"fmt"
"math/big"
@ -97,29 +95,6 @@ func (c *ClientsetCAStore) readCAKeypairs(id string) (*keyset, error) {
return keyset, nil
}
// generateCACertificate creates and stores a CA keypair
// Should be called with the mutex held, to prevent concurrent creation of different keys
func (c *ClientsetCAStore) generateCACertificate(id string) (*keyset, error) {
template := BuildCAX509Template()
caRsaKey, err := rsa.GenerateKey(crypto_rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("error generating RSA private key: %v", err)
}
caPrivateKey := &pki.PrivateKey{Key: caRsaKey}
t := time.Now().UnixNano()
template.SerialNumber = pki.BuildPKISerial(t)
caCertificate, err := pki.SignNewCertificate(caPrivateKey, template, nil, nil)
if err != nil {
return nil, err
}
return c.storeAndVerifyKeypair(id, caCertificate, caPrivateKey)
}
// keyset is a parsed Keyset
type keyset struct {
format KeysetFormat

View File

@ -44,19 +44,14 @@ func (s flavorList) Swap(i, j int) {
func (s flavorList) Less(i, j int) bool {
if s[i].VCPUs < s[j].VCPUs {
return true
} else {
if s[i].VCPUs > s[j].VCPUs {
return false
}
}
if s[i].VCPUs > s[j].VCPUs {
return false
}
return s[i].RAM < s[j].RAM
}
func (c *openstackCloud) DefaultInstanceType(cluster *kops.Cluster, ig *kops.InstanceGroup) (string, error) {
type flavorInfo struct {
memory int
cpu int
}
flavorPage, err := flavors.ListDetail(c.ComputeClient(), flavors.ListOpts{
MinRAM: 1024,
}).AllPages()

View File

@ -18,8 +18,6 @@ package fi
import (
"bytes"
crypto_rand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
@ -149,57 +147,6 @@ func BuildCAX509Template() *x509.Certificate {
return template
}
// Creates and stores CA keypair
// Should be called with the mutex held, to prevent concurrent creation of different keys
func (c *VFSCAStore) generateCACertificate(name string) (*keyset, *keyset, error) {
template := BuildCAX509Template()
caRsaKey, err := rsa.GenerateKey(crypto_rand.Reader, 2048)
if err != nil {
return nil, nil, fmt.Errorf("error generating RSA private key: %v", err)
}
caPrivateKey := &pki.PrivateKey{Key: caRsaKey}
caCertificate, err := pki.SignNewCertificate(caPrivateKey, template, nil, nil)
if err != nil {
return nil, nil, err
}
serial := c.SerialGenerator().String()
err = c.storePrivateKey(name, &keysetItem{id: serial, privateKey: caPrivateKey})
if err != nil {
return nil, nil, err
}
// Make double-sure it round-trips
privateKeys, err := c.loadPrivateKeys(c.buildPrivateKeyPoolPath(name), true)
if err != nil {
return nil, nil, err
}
if privateKeys == nil || privateKeys.primary == nil || privateKeys.primary.id != serial {
return nil, nil, fmt.Errorf("failed to round-trip CA private key")
}
err = c.storeCertificate(name, &keysetItem{id: serial, certificate: caCertificate})
if err != nil {
return nil, nil, err
}
// Make double-sure it round-trips
certificates, err := c.loadCertificates(c.buildCertificatePoolPath(name), true)
if err != nil {
return nil, nil, err
}
if certificates == nil || certificates.primary == nil || certificates.primary.id != serial {
return nil, nil, fmt.Errorf("failed to round-trip CA certifiacate")
}
return certificates, privateKeys, nil
}
func (c *VFSCAStore) buildCertificatePoolPath(name string) vfs.Path {
return c.basedir.Join("issued", name)
}
@ -1144,21 +1091,6 @@ func (c *VFSCAStore) FindSSHPublicKeys(name string) ([]*kops.SSHCredential, erro
return items, nil
}
func (c *VFSCAStore) loadData(p vfs.Path) (*pki.PrivateKey, error) {
data, err := p.ReadFile()
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
k, err := pki.ParsePEMPrivateKey(data)
if err != nil {
return nil, fmt.Errorf("error parsing private key from %q: %v", p, err)
}
return k, err
}
// DeleteKeysetItem implements CAStore::DeleteKeysetItem
func (c *VFSCAStore) DeleteKeysetItem(item *kops.Keyset, id string) error {
switch item.Spec.Type {