<commit_before>package config

//go:generate go run templates_gen.go
//go:generate gofmt -w templates.go

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net"
	"strings"
	"text/template"
	"unicode/utf8"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/kms"

	"github.com/coreos/coreos-cloudinit/config/validate"
	yaml "gopkg.in/yaml.v2"
)

const (
	credentialsDir = "credentials"
	userDataDir    = "userdata"
)

func newDefaultCluster() *Cluster {
	return &Cluster{
		ClusterName:              "kubernetes",
		ReleaseChannel:           "alpha",
		VPCCIDR:                  "10.0.0.0/16",
		ControllerIP:             "10.0.0.50",
		PodCIDR:                  "10.2.0.0/16",
		ServiceCIDR:              "10.3.0.0/24",
		DNSServiceIP:             "10.3.0.10",
		K8sVer:                   "v1.2.4_coreos.1",
		HyperkubeImageRepo:       "quay.io/coreos/hyperkube",
		ControllerInstanceType:   "m3.medium",
		ControllerRootVolumeSize: 30,
		WorkerCount:              1,
		WorkerInstanceType:       "m3.medium",
		WorkerRootVolumeSize:     30,
		CreateRecordSet:          false,
		RecordSetTTL:             300,
		Subnets:                  []Subnet{},
	}
}

func ClusterFromFile(filename string) (*Cluster, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	c, err := ClusterFromBytes(data)
	if err != nil {
		return nil, fmt.Errorf("file %s: %v", filename, err)
	}

	return c, nil
}

// ClusterFromBytes Necessary for unit tests, which store configs as hardcoded strings
func ClusterFromBytes(data []byte) (*Cluster, error) {
	c := newDefaultCluster()
	if err := yaml.Unmarshal(data, c); err != nil {
		return nil, fmt.Errorf("failed to parse cluster: %v", err)
	}

	// HostedZone needs to end with a '.', amazon will not append it for you.
	// as it will with RecordSets
	c.HostedZone = WithTrailingDot(c.HostedZone)

	// If the user specified no subnets, we assume that a single AZ configuration with the default instanceCIDR is demanded
	if len(c.Subnets) == 0 && c.InstanceCIDR == "" {
		c.InstanceCIDR = "10.0.0.0/24"
	}

	c.HostedZoneID = withHostedZoneIDPrefix(c.HostedZoneID)

	if err := c.valid(); err != nil {
		return nil, fmt.Errorf("invalid cluster: %v", err)
	}

	// For backward-compatibility
	if len(c.Subnets) == 0 {
		c.Subnets = []Subnet{
			{
				AvailabilityZone: c.AvailabilityZone,
				InstanceCIDR:     c.InstanceCIDR,
			},
		}
	}
	return c, nil
}

type Cluster struct {
	ClusterName              string            `yaml:"clusterName"`
	ExternalDNSName          string            `yaml:"externalDNSName"`
	KeyName                  string            `yaml:"keyName"`
	Region                   string            `yaml:"region"`
	AvailabilityZone         string            `yaml:"availabilityZone"`
	ReleaseChannel           string            `yaml:"releaseChannel"`
	ControllerInstanceType   string            `yaml:"controllerInstanceType"`
	ControllerRootVolumeSize int               `yaml:"controllerRootVolumeSize"`
	WorkerCount              int               `yaml:"workerCount"`
	WorkerInstanceType       string            `yaml:"workerInstanceType"`
	WorkerRootVolumeSize     int               `yaml:"workerRootVolumeSize"`
	WorkerSpotPrice          string            `yaml:"workerSpotPrice"`
	VPCID                    string            `yaml:"vpcId"`
	RouteTableID             string            `yaml:"routeTableId"`
	VPCCIDR                  string            `yaml:"vpcCIDR"`
	InstanceCIDR             string            `yaml:"instanceCIDR"`
	ControllerIP             string            `yaml:"controllerIP"`
	PodCIDR                  string            `yaml:"podCIDR"`
	ServiceCIDR              string            `yaml:"serviceCIDR"`
	DNSServiceIP             string            `yaml:"dnsServiceIP"`
	K8sVer                   string            `yaml:"kubernetesVersion"`
	HyperkubeImageRepo       string            `yaml:"hyperkubeImageRepo"`
	KMSKeyARN                string            `yaml:"kmsKeyArn"`
	CreateRecordSet          bool              `yaml:"createRecordSet"`
	RecordSetTTL             int               `yaml:"recordSetTTL"`
	HostedZone               string            `yaml:"hostedZone"`
	HostedZoneID             string            `yaml:"hostedZoneId"`
	StackTags                map[string]string `yaml:"stackTags"`
	UseCalico                bool              `yaml:"useCalico"`
	Subnets                  []Subnet          `yaml:"subnets"`
}

type Subnet struct {
	AvailabilityZone string `yaml:"availabilityZone"`
	InstanceCIDR     string `yaml:"instanceCIDR"`
}

const (
	vpcLogicalName = "VPC"
)

var supportedReleaseChannels = map[string]bool{
	"alpha":  true,
	"beta":   true,
	"stable": false,
}

func (c Cluster) Config() (*Config, error) {
	config := Config{Cluster: c}
	config.ETCDEndpoints = fmt.Sprintf("http://%s:2379", c.ControllerIP)
	config.APIServers = fmt.Sprintf("http://%s:8080", c.ControllerIP)
	config.SecureAPIServers = fmt.Sprintf("https://%s:443", c.ControllerIP)
	config.APIServerEndpoint = fmt.Sprintf("https://%s", c.ExternalDNSName)
	if config.UseCalico {
		config.K8sNetworkPlugin = "cni"
	}<commit_msg>config: add omitempty yaml tags for Cluster<commit_after>package config

//go:generate go run templates_gen.go
//go:generate gofmt -w templates.go

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net"
	"strings"
	"text/template"
	"unicode/utf8"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/kms"

	"github.com/coreos/coreos-cloudinit/config/validate"
	yaml "gopkg.in/yaml.v2"
)

const (
	credentialsDir = "credentials"
	userDataDir    = "userdata"
)

func newDefaultCluster() *Cluster {
	return &Cluster{
		ClusterName:              "kubernetes",
		ReleaseChannel:           "alpha",
		VPCCIDR:                  "10.0.0.0/16",
		ControllerIP:             "10.0.0.50",
		PodCIDR:                  "10.2.0.0/16",
		ServiceCIDR:              "10.3.0.0/24",
		DNSServiceIP:             "10.3.0.10",
		K8sVer:                   "v1.2.4_coreos.1",
		HyperkubeImageRepo:       "quay.io/coreos/hyperkube",
		ControllerInstanceType:   "m3.medium",
		ControllerRootVolumeSize: 30,
		WorkerCount:              1,
		WorkerInstanceType:       "m3.medium",
		WorkerRootVolumeSize:     30,
		CreateRecordSet:          false,
		RecordSetTTL:             300,
		Subnets:                  []Subnet{},
	}
}

func ClusterFromFile(filename string) (*Cluster, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	c, err := ClusterFromBytes(data)
	if err != nil {
		return nil, fmt.Errorf("file %s: %v", filename, err)
	}

	return c, nil
}

// ClusterFromBytes Necessary for unit tests, which store configs as hardcoded strings
func ClusterFromBytes(data []byte) (*Cluster, error) {
	c := newDefaultCluster()
	if err := yaml.Unmarshal(data, c); err != nil {
		return nil, fmt.Errorf("failed to parse cluster: %v", err)
	}

	// HostedZone needs to end with a '.', amazon will not append it for you.
	// as it will with RecordSets
	c.HostedZone = WithTrailingDot(c.HostedZone)

	// If the user specified no subnets, we assume that a single AZ configuration with the default instanceCIDR is demanded
	if len(c.Subnets) == 0 && c.InstanceCIDR == "" {
		c.InstanceCIDR = "10.0.0.0/24"
	}

	c.HostedZoneID = withHostedZoneIDPrefix(c.HostedZoneID)

	if err := c.valid(); err != nil {
		return nil, fmt.Errorf("invalid cluster: %v", err)
	}

	// For backward-compatibility
	if len(c.Subnets) == 0 {
		c.Subnets = []Subnet{
			{
				AvailabilityZone: c.AvailabilityZone,
				InstanceCIDR:     c.InstanceCIDR,
			},
		}
	}
	return c, nil
}

type Cluster struct {
	ClusterName              string            `yaml:"clusterName,omitempty"`
	ExternalDNSName          string            `yaml:"externalDNSName,omitempty"`
	KeyName                  string            `yaml:"keyName,omitempty"`
	Region                   string            `yaml:"region,omitempty"`
	AvailabilityZone         string            `yaml:"availabilityZone,omitempty"`
	ReleaseChannel           string            `yaml:"releaseChannel,omitempty"`
	ControllerInstanceType   string            `yaml:"controllerInstanceType,omitempty"`
	ControllerRootVolumeSize int               `yaml:"controllerRootVolumeSize,omitempty"`
	WorkerCount              int               `yaml:"workerCount,omitempty"`
	WorkerInstanceType       string            `yaml:"workerInstanceType,omitempty"`
	WorkerRootVolumeSize     int               `yaml:"workerRootVolumeSize,omitempty"`
	WorkerSpotPrice          string            `yaml:"workerSpotPrice,omitempty"`
	VPCID                    string            `yaml:"vpcId,omitempty"`
	RouteTableID             string            `yaml:"routeTableId,omitempty"`
	VPCCIDR                  string            `yaml:"vpcCIDR,omitempty"`
	InstanceCIDR             string            `yaml:"instanceCIDR,omitempty"`
	ControllerIP             string            `yaml:"controllerIP,omitempty"`
	PodCIDR                  string            `yaml:"podCIDR,omitempty"`
	ServiceCIDR              string            `yaml:"serviceCIDR,omitempty"`
	DNSServiceIP             string            `yaml:"dnsServiceIP,omitempty"`
	K8sVer                   string            `yaml:"kubernetesVersion,omitempty"`
	HyperkubeImageRepo       string            `yaml:"hyperkubeImageRepo,omitempty"`
	KMSKeyARN                string            `yaml:"kmsKeyArn,omitempty"`
	CreateRecordSet          bool              `yaml:"createRecordSet,omitempty"`
	RecordSetTTL             int               `yaml:"recordSetTTL,omitempty"`
	HostedZone               string            `yaml:"hostedZone,omitempty"`
	HostedZoneID             string            `yaml:"hostedZoneId,omitempty"`
	StackTags                map[string]string `yaml:"stackTags,omitempty"`
	UseCalico                bool              `yaml:"useCalico,omitempty"`
	Subnets                  []Subnet          `yaml:"subnets,omitempty"`
}

type Subnet struct {
	AvailabilityZone string `yaml:"availabilityZone,omitempty"`
	InstanceCIDR     string `yaml:"instanceCIDR,omitempty"`
}

const (
	vpcLogicalName = "VPC"
)

var supportedReleaseChannels = map[string]bool{
	"alpha":  true,
	"beta":   true,
	"stable": false,
}

func (c Cluster) Config() (*Config, error) {
	config := Config{Cluster: c}
	config.ETCDEndpoints = fmt.Sprintf("http://%s:2379", c.ControllerIP)
	config.APIServers = fmt.Sprintf("http://%s:8080", c.ControllerIP)
	config.SecureAPIServers = fmt.Sprintf("https://%s:443", c.ControllerIP)
	config.APIServerEndpoint = fmt.Sprintf("https://%s", c.ExternalDNSName)
	if config.UseCalico {
		config.K8sNetworkPlugin = "cni"
	}
{'fraction_non_alphanumeric': 0.1164804469273743, 'fraction_numerical': 0.013407821229050279, 'mean_word_length': 2.378735451399811, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7109223', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3319, 'n_words': 692}