<commit_before>package esiimap

import (
	"context"
	"errors"
	"log"

	"github.com/antihax/goesi"
	"github.com/emersion/go-imap/backend"
	"golang.org/x/oauth2"
)

type User struct {
	username    string
	token       oauth2.TokenSource
	backend     *Backend
	characterID int32
	mailboxes   map[string]*Mailbox
}

func NewUser(username string, token oauth2.TokenSource, backend *Backend, characterID int32) *User {
	user := &User{
		username:    username,
		token:       token,
		backend:     backend,
		characterID: characterID,
		mailboxes:   make(map[string]*Mailbox),
	}
	user.loadMailboxes()
	return user
}

func (u *User) Username() string {
	return u.username
}

func (u *User) loadMailboxes() error {
	// Retreive all the mailboxes from ESI
	auth := context.WithValue(context.Background(), goesi.ContextOAuth2, u.token)
	boxes, _, err := u.backend.esi.ESI.MailApi.GetCharactersCharacterIdMailLabels(auth, u.characterID, nil)
	if err != nil {
		log.Println(err)
		return err
	}

	// Create and load all the mailboxes in the background
	for _, box := range boxes.Labels {
		u.mailboxes[box.Name] = NewMailbox(box.Name, box.LabelId, u, box.UnreadCount)
	}

	go func() {
		// Retreive mailing lists
		mailingLists, _, err := u.backend.esi.ESI.MailApi.GetCharactersCharacterIdMailLists(auth, u.characterID, nil)
		if err != nil {
			log.Println(err)
			return
		}
		// Cache the mail lists
		if len(mailingLists) > 0 {
			go u.cacheMailingLists(mailingLists)
		}
	}()

	return nil
}

func (u *User) ListMailboxes(subscribed bool) (mailboxes []backend.Mailbox, err error) {
	for _, box := range u.mailboxes {
		mailboxes = append(mailboxes, box)
	}
	return mailboxes, nil
}

func (u *User) GetMailbox(name string) (backend.Mailbox, error) {
	mailbox, ok := u.mailboxes[name]
	if !ok {
		log.Printf("Cant find mailbox %s", name)
		return mailbox, errors.New("No such mailbox")
	}
	mailbox.Load()
	mailbox.WaitForLoad()
	return mailbox, nil
}

func (u *User) CreateMailbox(name string) error {
	return errors.New("You cannot create mailboxes")
}

func (u *User) DeleteMailbox(name string) error {
	return errors.New("You cannot delete mailboxes")
}

func (u *User) RenameMailbox(existingName, newName string) error {
	return errors.New("You cannot rename mailboxes")
}

func (u *User) Logout() error {
	return nil
}
<commit_msg>Why You do dis?!<commit_after>package esiimap

import (
	"context"
	"errors"
	"log"

	"github.com/antihax/goesi"
	"github.com/emersion/go-imap/backend"
	"golang.org/x/oauth2"
)

type User struct {
	username    string
	token       oauth2.TokenSource
	backend     *Backend
	characterID int32
	mailboxes   map[string]*Mailbox
}

func NewUser(username string, token oauth2.TokenSource, backend *Backend, characterID int32) *User {
	user := &User{
		username:    username,
		token:       token,
		backend:     backend,
		characterID: characterID,
		mailboxes:   make(map[string]*Mailbox),
	}
	user.loadMailboxes()
	return user
}

func (u *User) Username() string {
	return u.username
}

func (u *User) loadMailboxes() error {
	// Retreive all the mailboxes from ESI
	auth := context.WithValue(context.Background(), goesi.ContextOAuth2, u.token)
	boxes, _, err := u.backend.esi.ESI.MailApi.GetCharactersCharacterIdMailLabels(auth, u.characterID, nil)
	if err != nil {
		log.Println(err)
		return err
	}

	// Create and load all the mailboxes in the background
	for _, box := range boxes.Labels {
		u.mailboxes[box.Name] = NewMailbox(box.Name, box.LabelId, u, box.UnreadCount)
	}

	go func() {
		// Retreive mailing lists
		mailingLists, _, err := u.backend.esi.ESI.MailApi.GetCharactersCharacterIdMailLists(auth, u.characterID, nil)
		if err != nil {
			log.Println(err)
			return
		}
		// Cache the mail lists
		if len(mailingLists) > 0 {
			go u.cacheMailingLists(mailingLists)
		}
	}()

	return nil
}

func (u *User) ListMailboxes(subscribed bool) (mailboxes []backend.Mailbox, err error) {
	for _, box := range u.mailboxes {
		mailboxes = append(mailboxes, box)
	}
	return mailboxes, nil
}

func (u *User) GetMailbox(name string) (backend.Mailbox, error) {
	// WHY?!
	if name == "INBOX" {
		name = "Inbox"
	}
	mailbox, ok := u.mailboxes[name]
	if !ok {
		log.Printf("Cant find mailbox %s", name)
		return mailbox, errors.New("No such mailbox")
	}
	mailbox.Load()
	mailbox.WaitForLoad()
	return mailbox, nil
}

func (u *User) CreateMailbox(name string) error {
	return errors.New("You cannot create mailboxes")
}

func (u *User) DeleteMailbox(name string) error {
	return errors.New("You cannot delete mailboxes")
}

func (u *User) RenameMailbox(existingName, newName string) error {
	return errors.New("You cannot rename mailboxes")
}

func (u *User) Logout() error {
	return nil
}
<|endoftext|>


<commit_before>/*
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 main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/juju/ratelimit"
	"google.golang.org/api/googleapi"
	"google.golang.org/api/youtube/v3"
)

var (
	filename     = flag.String("filename", "", "Filename to upload. Can be a URL")
	title        = flag.String("title", "Video Title", "Video title")
	description  = flag.String("description", "uploaded by youtubeuploader", "Video description")
	category     = flag.String("category", "", "Video category")
	keywords     = flag.String("keywords", "", "Comma separated list of video keywords")
	privacy      = flag.String("privacy", "private", "Video privacy status")
	showProgress = flag.Bool("progress", true, "Show progress indicator")
	rate         = flag.Int("ratelimit", 0, "Rate limit upload in KB/s. No limit by default")
)

type customReader struct {
	Reader io.Reader

	bytes     int64
	lapTime   time.Time
	startTime time.Time
	fileSize  int64
}

func main() {
	flag.Parse()

	if *filename == "" {
		log.Fatalf("You must provide a filename of a video file to upload")
	}

	client, err := buildOAuthHTTPClient(youtube.YoutubeUploadScope)
	if err != nil {
		log.Fatalf("Error building OAuth client: %v", err)
	}

	service, err := youtube.New(client)
	if err != nil {
		log.Fatalf("Error creating YouTube client: %v", err)
	}

	upload := &youtube.Video{
		Snippet: &youtube.VideoSnippet{
			Title:       *title,
			Description: *description,
			CategoryId:  *category,
		},
		Status: &youtube.VideoStatus{PrivacyStatus: *privacy},
	}

	// The API returns a 400 Bad Request response if tags is an empty string.
	if strings.Trim(*keywords, "") != "" {
		upload.Snippet.Tags = strings.Split(*keywords, ",")
	}

	call := service.Videos.Insert("snippet,status", upload)

	reader := &customReader{}
	var lreader io.Reader

	if *rate > 0 {
		// Bucket adding rate KB every second, holding max 100KB
		bucket := ratelimit.NewBucketWithRate(float64(*rate)*1024, 100*1024)
		lreader = ratelimit.Reader(reader, bucket)
	}

	if strings.HasPrefix(*filename, "http") {
		resp, err := http.Head(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		lenStr := resp.Header.Get("content-length")
		if lenStr != "" {
			reader.fileSize, err = strconv.ParseInt(lenStr, 10, 64)
			if err != nil {
				log.Fatal(err)
			}
		}

		resp, err = http.Get(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		reader.Reader = resp.Body
		reader.fileSize = resp.ContentLength
		defer resp.Body.Close()
	} else {
		file, err := os.Open(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		fileInfo, err := file.Stat()
		if err != nil {
			log.Fatalf("Error stating file %v: %v", *filename, err)
		}
		reader.fileSize = fileInfo.Size()
		reader.Reader = file
		defer file.Close()
	}

	var option googleapi.MediaOption
	if reader.fileSize < (1024 * 1024 * 10) {
		// on small uploads (<10MB), set minimum chunk size so we can see progress
		option = googleapi.ChunkSize(1)
	} else {
		// on larger uploads, use the default chunk size for best performance
		option = googleapi.ChunkSize(googleapi.DefaultUploadChunkSize)
	}

	var video *youtube.Video
	if lreader != nil {
		// rate-limited reader
		video, err = call.Media(lreader, option).Do()
	} else {
		video, err = call.Media(reader, option).Do()
	}
	if err != nil {
		if video != nil {
			log.Fatalf("Error making YouTube API call: %v, %v", err, video.HTTPStatusCode)
		} else {
			log.Fatalf("Error making YouTube API call: %v", err)
		}
	}
	fmt.Printf("\nUpload successful! Video ID: %v\n", video.Id)
}

func (r *customReader) progress(Bps int64) {
	if r.fileSize > 0 {
		eta := time.Duration((r.fileSize-r.bytes)/Bps) * time.Second
		fmt.Printf("\rTransfer rate %.2f Mbps, %d / %d (%.2f%%) ETA %s", float32(Bps*8)/(1000*1000), r.bytes, r.fileSize, float32(r.bytes)/float32(r.fileSize)*100, eta)
	} else {
		fmt.Printf("\rTransfer rate %.2f Mbps, %d", float32(Bps*8)/(1000*1000), r.bytes)
	}
}

func (r *customReader) Read(p []byte) (n int, err error) {
	if r.startTime.IsZero() {
		r.startTime = time.Now()
	}
	if r.lapTime.IsZero() {
		r.lapTime = time.Now()
	}
	if len(p) == 0 {
		return 0, nil
	}
	n, err = r.Reader.Read(p)
	r.bytes += int64(n)

	if time.Since(r.lapTime) >= time.Second || err == io.EOF {
		timeSince := int64(time.Since(r.startTime).Seconds())
		if timeSince == 0 {
			r.progress(r.bytes)
		} else {
			r.progress(r.bytes / timeSince)
		}
		r.lapTime = time.Now()
	}

	return n, err
}
<commit_msg>initial commit<commit_after>/*
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 main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/mxk/go-flowrate/flowrate"
	"google.golang.org/api/googleapi"
	"google.golang.org/api/youtube/v3"
)

var (
	filename     = flag.String("filename", "", "Filename to upload. Can be a URL")
	title        = flag.String("title", "Video Title", "Video title")
	description  = flag.String("description", "uploaded by youtubeuploader", "Video description")
	category     = flag.String("category", "", "Video category")
	keywords     = flag.String("keywords", "", "Comma separated list of video keywords")
	privacy      = flag.String("privacy", "private", "Video privacy status")
	showProgress = flag.Bool("progress", true, "Show progress indicator")
	rate         = flag.Int("ratelimit", 0, "Rate limit upload in KB/s. No limit by default")
)

type customReader struct {
	Reader io.Reader

	bytes     int64
	lapTime   time.Time
	startTime time.Time
	filesize  int64
}

func main() {
	flag.Parse()

	if *filename == "" {
		log.Fatalf("You must provide a filename of a video file to upload")
	}

	var reader io.Reader
	var filesize int64

	if strings.HasPrefix(*filename, "http") {
		resp, err := http.Head(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		lenStr := resp.Header.Get("content-length")
		if lenStr != "" {
			filesize, err = strconv.ParseInt(lenStr, 10, 64)
			if err != nil {
				log.Fatal(err)
			}
		}

		resp, err = http.Get(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		reader = resp.Body
		filesize = resp.ContentLength
		defer resp.Body.Close()
	} else {
		file, err := os.Open(*filename)
		if err != nil {
			log.Fatalf("Error opening %v: %v", *filename, err)
		}
		fileInfo, err := file.Stat()
		if err != nil {
			log.Fatalf("Error stating file %v: %v", *filename, err)
		}
		filesize = fileInfo.Size()
		reader = file
		defer file.Close()
	}

	var option googleapi.MediaOption
	if filesize < (1024 * 1024 * 10) {
		// on small uploads (<10MB), set minimum chunk size so we can see progress
		option = googleapi.ChunkSize(1)
	} else {
		// on larger uploads, use the default chunk size for best performance
		option = googleapi.ChunkSize(googleapi.DefaultUploadChunkSize)
	}

	transport := limitTransport{}
	transport.filesize = filesize
	client, err := buildOAuthHTTPClient(youtube.YoutubeUploadScope)
	if err != nil {
		log.Fatalf("Error building OAuth client: %v", err)
	}
	client.Transport = transport

	service, err := youtube.New(client)
	if err != nil {
		log.Fatalf("Error creating YouTube client: %v", err)
	}

	upload := &youtube.Video{
		Snippet: &youtube.VideoSnippet{
			Title:       *title,
			Description: *description,
			CategoryId:  *category,
		},
		Status: &youtube.VideoStatus{PrivacyStatus: *privacy},
	}

	// The API returns a 400 Bad Request response if tags is an empty string.
	if strings.Trim(*keywords, "") != "" {
		upload.Snippet.Tags = strings.Split(*keywords, ",")
	}

	call := service.Videos.Insert("snippet,status", upload)

	var video *youtube.Video
	video, err = call.Media(reader, option).Do()
	if err != nil {
		if video != nil {
			log.Fatalf("Error making YouTube API call: %v, %v", err, video.HTTPStatusCode)
		} else {
			log.Fatalf("Error making YouTube API call: %v", err)
		}
	}
	fmt.Printf("\nUpload successful! Video ID: %v\n", video.Id)
}

type limitTransport struct {
	http.RoundTripper
	filesize int64
}

func (t limitTransport) RoundTrip(r *http.Request) (res *http.Response, err error) {
	body := flowrate.NewReader(r.Body, int64(*rate))
	body.Monitor.SetTransferSize(t.filesize)
	r.Body = body
	return t.RoundTripper.RoundTrip(r)
}
<|endoftext|>


<commit_before>package podcasts

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/stretchr/testify/suite"
)

type PodcastsTestSuite struct {
	suite.Suite
	podcast *Podcast
}

func (s *PodcastsTestSuite) SetupTest() {
	s.podcast = &Podcast{
		Title:       "my podcast title",
		Description: "my podcast description",
		Language:    "my podcast lang",
		Link:        "my podcast link",
		Copyright:   "my podcast copyright",
	}
}

func (s *PodcastsTestSuite) TestContainsXmlHeader() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<?xml version="1.0" encoding="UTF-8"?>`)
}

func (s *PodcastsTestSuite) TestContainsRssElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">`)
}

func (s *PodcastsTestSuite) TestContainsChannelElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<channel>`)
	s.Contains(data, `</channel>`)
}

func (s *PodcastsTestSuite) TestContainsTitleElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<title>%s</title>", s.podcast.Title))
}

func (s *PodcastsTestSuite) TestContainsDescriptionElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<description>%s</description>", s.podcast.Description))
}

func (s *PodcastsTestSuite) TestContainsLanguageElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<language>%s</language>", s.podcast.Language))
}

func (s *PodcastsTestSuite) TestContainsLinkElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<link>%s</link>", s.podcast.Link))
}

func (s *PodcastsTestSuite) TestContainsCopyrightElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<copyright>%s</copyright>", s.podcast.Copyright))
}

func (s *PodcastsTestSuite) TestPodcastFeedWrite() {
	feed, err := s.podcast.Feed()
	s.Nil(err)

	var b bytes.Buffer
	err = feed.Write(&b)
	s.Nil(err)
}

func TestPodcastsTestSuite(t *testing.T) {
	suite.Run(t, new(PodcastsTestSuite))
}

func getPodcastXML(p *Podcast) (string, error) {
	feed, err := p.Feed()
	if err != nil {
		return "", err
	}
	return feed.XML()
}
<commit_msg>update test<commit_after>package podcasts

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/stretchr/testify/suite"
)

type PodcastsTestSuite struct {
	suite.Suite
	podcast *Podcast
}

func (s *PodcastsTestSuite) SetupTest() {
	s.podcast = &Podcast{
		Title:       "my podcast title",
		Description: "my podcast description",
		Language:    "my podcast lang",
		Link:        "my podcast link",
		Copyright:   "my podcast copyright",
	}
}

func (s *PodcastsTestSuite) TestContainsXmlHeader() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<?xml version="1.0" encoding="UTF-8"?>`)
}

func (s *PodcastsTestSuite) TestContainsRssElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">`)
}

func (s *PodcastsTestSuite) TestContainsChannelElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, `<channel>`)
	s.Contains(data, `</channel>`)
}

func (s *PodcastsTestSuite) TestContainsTitleElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<title>%s</title>", s.podcast.Title))
}

func (s *PodcastsTestSuite) TestContainsDescriptionElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<description>%s</description>", s.podcast.Description))
}

func (s *PodcastsTestSuite) TestContainsLanguageElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<language>%s</language>", s.podcast.Language))
}

func (s *PodcastsTestSuite) TestContainsLinkElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<link>%s</link>", s.podcast.Link))
}

func (s *PodcastsTestSuite) TestContainsCopyrightElement() {
	data, err := getPodcastXML(s.podcast)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<copyright>%s</copyright>", s.podcast.Copyright))
}

func (s *PodcastsTestSuite) TestContainsBlockElement() {
	data, err := getPodcastXML(s.podcast, Block)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:block>%s</itunes:block>", ValueYes))
}

func (s *PodcastsTestSuite) TestContainsExplicitElement() {
	data, err := getPodcastXML(s.podcast, Explicit)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:explicit>%s</itunes:explicit>", ValueYes))
}

func (s *PodcastsTestSuite) TestContainsCompleteElement() {
	data, err := getPodcastXML(s.podcast, Complete)

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:complete>%s</itunes:complete>", ValueYes))
}

func (s *PodcastsTestSuite) TestContainsAuthorElement() {
	author := "Test Author"
	data, err := getPodcastXML(s.podcast, Author(author))

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:author>%s</itunes:author>", author))
}

func (s *PodcastsTestSuite) TestContainsNewFeedURLElement() {
	url := "http://localhost/my-test-url"
	data, err := getPodcastXML(s.podcast, NewFeedURL(url))

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:new-feed-url>%s</itunes:new-feed-url>", url))
}

func (s *PodcastsTestSuite) TestContainsSubtitleElement() {
	subtitle := "Test Subtitle"
	data, err := getPodcastXML(s.podcast, Subtitle(subtitle))

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:subtitle>%s</itunes:subtitle>", subtitle))
}

func (s *PodcastsTestSuite) TestContainsSummaryElement() {
	summary := "Test Subtitle"
	data, err := getPodcastXML(s.podcast, Summary(summary))

	s.Nil(err)
	s.Contains(data, fmt.Sprintf("<itunes:summary>%s</itunes:summary>", summary))
}

func (s *PodcastsTestSuite) TestContainsOwnerElement() {
	name := "Test Name"
	email := "test@name.com"
	data, err := getPodcastXML(s.podcast, Owner(name, email))

	s.Nil(err)
	s.Contains(data, "<itunes:owner>")
	s.Contains(data, fmt.Sprintf("<itunes:name>%s</itunes:name>", name))
	s.Contains(data, fmt.Sprintf("<itunes:email>%s</itunes:email>", email))
	s.Contains(data, "</itunes:owner>")
}

func (s *PodcastsTestSuite) TestContainsImageElement() {
	image := "http://localhost/myimage.jpg"
	data, err := getPodcastXML(s.podcast, Image(image))

	s.Nil(err)
	s.Contains(data, fmt.Sprintf(`<itunes:image href="%s"></itunes:image>`, image))
}

func (s *PodcastsTestSuite) TestPodcastFeedWrite() {
	feed, err := s.podcast.Feed()
	s.Nil(err)

	var b bytes.Buffer
	err = feed.Write(&b)
	s.Nil(err)
}

func TestPodcastsTestSuite(t *testing.T) {
	suite.Run(t, new(PodcastsTestSuite))
}

func getPodcastXML(p *Podcast, options ...func(f *Feed) error) (string, error) {
	feed, err := p.Feed(options...)
	if err != nil {
		return "", err
	}
	return feed.XML()
}
<|endoftext|>


<commit_before>package ratelimiter

import (
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestDecorator(t *testing.T) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-Path", r.URL.Path)
		w.Write([]byte("Testing 1 2 3"))
	})

	rl := func(a *http.Request) (ok bool, retryAfter time.Duration) {
		return false, time.Minute
	}

	testlog := Decorate(h, rl)

	ts := httptest.NewServer(testlog)
	defer ts.Close()

	res, err := http.Get(ts.URL + "/foo/bar")
	if err != nil {
		t.Fatal(err)
	}
	if retryAfter := res.Header.Get("Retry-After"); retryAfter != "60" {
		t.Fatalf("Expected retry-after to be '60' but it was '%s'", retryAfter)
	}
	res.Body.Close()
}
<commit_msg>Use 'error' not 'fatal' in test failures<commit_after>package ratelimiter

import (
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestDecorator(t *testing.T) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-Path", r.URL.Path)
		w.Write([]byte("Testing 1 2 3"))
	})

	rl := func(a *http.Request) (ok bool, retryAfter time.Duration) {
		return false, time.Minute
	}

	testlog := Decorate(h, rl)

	ts := httptest.NewServer(testlog)
	defer ts.Close()

	res, err := http.Get(ts.URL + "/foo/bar")
	if err != nil {
		t.Error(err)
	}
	if retryAfter := res.Header.Get("Retry-After"); retryAfter != "60" {
		t.Errorf("Expected retry-after to be '60' but it was '%s'", retryAfter)
	}
	res.Body.Close()
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/lhz/breadbox/gfx"

	"fmt"
	"image"
	"image/png"
	"log"
	"os"
)

var palette = gfx.Pepto

var mask1 = []byte{128, 32, 8, 2}
var mask2 = []byte{ 64, 16, 4, 1}
var c byte

func main() {
	if len(os.Args) != 3 {
		fmt.Fprintf(os.Stderr, "Usage: %v <source> <target>\n", os.Args[0])
		os.Exit(1)
	}

	sourceFile, targetFile := os.Args[1], os.Args[2]

	koala := make([]byte, 10003)
	f, err := os.Open(sourceFile)
	if err != nil {
		log.Fatalf("Can't open file %s for reading: %v", sourceFile, err)
		return
	}

	_, err = f.Read(koala)
	if err != nil {
		log.Fatalf("Can't read from file %s: %v", sourceFile, err)
		return
	}

    img := image.NewPaletted(image.Rect(0, 0, 320, 200), gfx.Pepto)

	bkg := koala[10002]

	for row := 0; row < 25; row++ {
		for col := 0; col < 40; col++ {
			scr := koala[8002 + row * 40 + col]
			cmp := koala[9002 + row * 40 + col]
			for y := 0; y < 8; y++ {
				byte := koala[2 + row * 320 + col * 8 + y]
				for x := 0; x < 4; x++ {
					b1, b2 := byte & mask1[x], byte & mask2[x]
					if b1 > 0 && b2 > 0 {
						c = cmp & 0x0F
					} else if b1 > 0 {
						c = scr & 0x0F
					} else if b2 > 0 {
						c = (scr & 0xF0) >> 4
					} else {
						c = bkg
					}
					img.Set(col * 8 + x * 2, row * 8 + y, palette[c])
					img.Set(col * 8 + x * 2 + 1, row * 8 + y, palette[c])
				}
			}
		}
	}

    f, _ = os.OpenFile(targetFile, os.O_WRONLY|os.O_CREATE, 0600)
    defer f.Close()
    png.Encode(f, img)
}
<commit_msg>Mask off upper nybble from background color.<commit_after>package main

import (
	"github.com/lhz/breadbox/gfx"

	"fmt"
	"image"
	"image/png"
	"log"
	"os"
)

var palette = gfx.Pepto

var mask1 = []byte{128, 32, 8, 2}
var mask2 = []byte{ 64, 16, 4, 1}
var c byte

func main() {
	if len(os.Args) != 3 {
		fmt.Fprintf(os.Stderr, "Usage: %v <source> <target>\n", os.Args[0])
		os.Exit(1)
	}

	sourceFile, targetFile := os.Args[1], os.Args[2]

	koala := make([]byte, 10003)
	f, err := os.Open(sourceFile)
	if err != nil {
		log.Fatalf("Can't open file %s for reading: %v", sourceFile, err)
		return
	}

	_, err = f.Read(koala)
	if err != nil {
		log.Fatalf("Can't read from file %s: %v", sourceFile, err)
		return
	}

    img := image.NewPaletted(image.Rect(0, 0, 320, 200), gfx.Pepto)

	bkg := koala[10002] & 0x0F

	for row := 0; row < 25; row++ {
		for col := 0; col < 40; col++ {
			scr := koala[8002 + row * 40 + col]
			cmp := koala[9002 + row * 40 + col]
			for y := 0; y < 8; y++ {
				byte := koala[2 + row * 320 + col * 8 + y]
				for x := 0; x < 4; x++ {
					b1, b2 := byte & mask1[x], byte & mask2[x]
					if b1 > 0 && b2 > 0 {
						c = cmp & 0x0F
					} else if b1 > 0 {
						c = scr & 0x0F
					} else if b2 > 0 {
						c = (scr & 0xF0) >> 4
					} else {
						c = bkg
					}
					img.Set(col * 8 + x * 2, row * 8 + y, palette[c])
					img.Set(col * 8 + x * 2 + 1, row * 8 + y, palette[c])
				}
			}
		}
	}

    f, _ = os.OpenFile(targetFile, os.O_WRONLY|os.O_CREATE, 0600)
    defer f.Close()
    png.Encode(f, img)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"garden-external-networker/cni"
	"garden-external-networker/config"
	"garden-external-networker/controller"
	"garden-external-networker/filelock"
	"garden-external-networker/port_allocator"
	"io/ioutil"
	"os"

	"github.com/coreos/go-iptables/iptables"

	"code.cloudfoundry.org/lager"
)

var (
	action            string
	handle            string
	cfg               config.Config
	encodedProperties string
	gardenNetworkSpec string
)

func parseArgs(allArgs []string) error {
	var configFilePath string

	flagSet := flag.NewFlagSet("", flag.ContinueOnError)

	flagSet.StringVar(&action, "action", "", "")
	flagSet.StringVar(&handle, "handle", "", "")
	flagSet.StringVar(&encodedProperties, "properties", "", "")
	flagSet.StringVar(&configFilePath, "configFile", "", "")

	err := flagSet.Parse(allArgs[1:])
	if err != nil {
		return err
	}
	if len(flagSet.Args()) > 0 {
		return fmt.Errorf("unexpected extra args: %+v", flagSet.Args())
	}

	if handle == "" {
		return fmt.Errorf("missing required flag 'handle'")
	}

	if configFilePath == "" {
		return fmt.Errorf("missing required flag 'configFile'")
	}

	cfg, err = config.New(configFilePath)
	if err != nil {
		return err
	}

	if action == "" {
		return fmt.Errorf("missing required flag 'action'")
	}

	return nil
}

func die(logger lager.Logger, action string, err error, data ...lager.Data) {
	logger.Error(action, err, data...)
	os.Exit(1)
}

func main() {
	logger := lager.NewLogger("garden-external-networker")
	logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.INFO))

	if len(os.Args) == 1 || os.Args[1] == "-h" || os.Args[1] == "--help" {
		fmt.Fprintf(os.Stderr, "this is used by garden-runc.  don't run it directly.")
		os.Exit(1)
	}

	inputBytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		die(logger, "read-stdin", err)
	}

	err = parseArgs(os.Args)
	if err != nil {
		die(logger, "parse-args", err)
	}

	var containerState struct {
		Pid int
	}
	if action == "up" {
		err = json.Unmarshal(inputBytes, &containerState)
		if err != nil {
			die(logger, "reading-stdin", err, lager.Data{"stdin": string(inputBytes)})
		}
	}

	cniLoader := &cni.CNILoader{
		PluginDir: cfg.CniPluginDir,
		ConfigDir: cfg.CniConfigDir,
		Logger:    logger,
	}

	networks, err := cniLoader.GetNetworkConfigs()
	if err != nil {
		die(logger, "load-cni-plugins", err)
	}

	cniController := &cni.CNIController{
		Logger:         logger,
		CNIConfig:      cniLoader.GetCNIConfig(),
		NetworkConfigs: networks,
	}

	mounter := &controller.Mounter{}

	ipt, err := iptables.New()
	if err != nil {
		die(logger, "iptables-new", err)
	}

	locker := &filelock.Locker{Path: cfg.StateFilePath}
	tracker := &port_allocator.Tracker{
		Logger:    logger,
		StartPort: cfg.StartPort,
		Capacity:  cfg.TotalPorts,
	}
	serializer := &port_allocator.Serializer{}
	portAllocator := &port_allocator.PortAllocator{
		Tracker:    tracker,
		Serializer: serializer,
		Locker:     locker,
	}

	manager := &controller.Manager{
		Logger:         logger,
		CNIController:  cniController,
		Mounter:        mounter,
		BindMountRoot:  cfg.BindMountDir,
		PortAllocator:  portAllocator,
		OverlayNetwork: cfg.OverlayNetwork,
		IPTables:       ipt,
	}

	logger.Info("action", lager.Data{"action": action})

	switch action {
	case "up":
		properties, err := manager.Up(containerState.Pid, handle, encodedProperties)
		if err != nil {
			die(logger, "manager-up", err)
		}
		err = json.NewEncoder(os.Stdout).Encode(map[string]interface{}{"properties": properties})
		if err != nil {
			die(logger, "writing-properties", err)
		}
	case "down":
		err = manager.Down(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-down", err)
		}
	case "net-out":
		err = manager.NetOut(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-net-out", err)
		}
	case "net-in":
		netInResult, err := manager.NetIn(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-net-in", err)
		}
		err = json.NewEncoder(os.Stdout).Encode(netInResult)
		if err != nil {
			die(logger, "writing-net-in-result", err)
		}
	default:
		die(logger, "unknown-action", fmt.Errorf("unrecognized action: %s", action))
	}
}
<commit_msg>Add back deprecated network flag<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"garden-external-networker/cni"
	"garden-external-networker/config"
	"garden-external-networker/controller"
	"garden-external-networker/filelock"
	"garden-external-networker/port_allocator"
	"io/ioutil"
	"os"

	"github.com/coreos/go-iptables/iptables"

	"code.cloudfoundry.org/lager"
)

var (
	action            string
	handle            string
	cfg               config.Config
	encodedProperties string
	gardenNetworkSpec string
	deprecatedNetwork string
)

func parseArgs(allArgs []string) error {
	var configFilePath string

	flagSet := flag.NewFlagSet("", flag.ContinueOnError)

	flagSet.StringVar(&action, "action", "", "")
	flagSet.StringVar(&handle, "handle", "", "")
	flagSet.StringVar(&deprecatedNetwork, "network", "", "")
	flagSet.StringVar(&encodedProperties, "properties", "", "")
	flagSet.StringVar(&configFilePath, "configFile", "", "")

	err := flagSet.Parse(allArgs[1:])
	if err != nil {
		return err
	}
	if len(flagSet.Args()) > 0 {
		return fmt.Errorf("unexpected extra args: %+v", flagSet.Args())
	}

	if handle == "" {
		return fmt.Errorf("missing required flag 'handle'")
	}

	if configFilePath == "" {
		return fmt.Errorf("missing required flag 'configFile'")
	}

	cfg, err = config.New(configFilePath)
	if err != nil {
		return err
	}

	if action == "" {
		return fmt.Errorf("missing required flag 'action'")
	}

	return nil
}

func die(logger lager.Logger, action string, err error, data ...lager.Data) {
	logger.Error(action, err, data...)
	os.Exit(1)
}

func main() {
	logger := lager.NewLogger("garden-external-networker")
	logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.INFO))

	if len(os.Args) == 1 || os.Args[1] == "-h" || os.Args[1] == "--help" {
		fmt.Fprintf(os.Stderr, "this is used by garden-runc.  don't run it directly.")
		os.Exit(1)
	}

	inputBytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		die(logger, "read-stdin", err)
	}

	err = parseArgs(os.Args)
	if err != nil {
		die(logger, "parse-args", err)
	}

	var containerState struct {
		Pid int
	}
	if action == "up" {
		err = json.Unmarshal(inputBytes, &containerState)
		if err != nil {
			die(logger, "reading-stdin", err, lager.Data{"stdin": string(inputBytes)})
		}
	}

	cniLoader := &cni.CNILoader{
		PluginDir: cfg.CniPluginDir,
		ConfigDir: cfg.CniConfigDir,
		Logger:    logger,
	}

	networks, err := cniLoader.GetNetworkConfigs()
	if err != nil {
		die(logger, "load-cni-plugins", err)
	}

	cniController := &cni.CNIController{
		Logger:         logger,
		CNIConfig:      cniLoader.GetCNIConfig(),
		NetworkConfigs: networks,
	}

	mounter := &controller.Mounter{}

	ipt, err := iptables.New()
	if err != nil {
		die(logger, "iptables-new", err)
	}

	locker := &filelock.Locker{Path: cfg.StateFilePath}
	tracker := &port_allocator.Tracker{
		Logger:    logger,
		StartPort: cfg.StartPort,
		Capacity:  cfg.TotalPorts,
	}
	serializer := &port_allocator.Serializer{}
	portAllocator := &port_allocator.PortAllocator{
		Tracker:    tracker,
		Serializer: serializer,
		Locker:     locker,
	}

	manager := &controller.Manager{
		Logger:         logger,
		CNIController:  cniController,
		Mounter:        mounter,
		BindMountRoot:  cfg.BindMountDir,
		PortAllocator:  portAllocator,
		OverlayNetwork: cfg.OverlayNetwork,
		IPTables:       ipt,
	}

	logger.Info("action", lager.Data{"action": action})

	switch action {
	case "up":
		properties, err := manager.Up(containerState.Pid, handle, encodedProperties)
		if err != nil {
			die(logger, "manager-up", err)
		}
		err = json.NewEncoder(os.Stdout).Encode(map[string]interface{}{"properties": properties})
		if err != nil {
			die(logger, "writing-properties", err)
		}
	case "down":
		err = manager.Down(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-down", err)
		}
	case "net-out":
		err = manager.NetOut(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-net-out", err)
		}
	case "net-in":
		netInResult, err := manager.NetIn(handle, encodedProperties)
		if err != nil {
			die(logger, "manager-net-in", err)
		}
		err = json.NewEncoder(os.Stdout).Encode(netInResult)
		if err != nil {
			die(logger, "writing-net-in-result", err)
		}
	default:
		die(logger, "unknown-action", fmt.Errorf("unrecognized action: %s", action))
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2015 The Kubernetes Authors All rights reserved.

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 componentconfig

import "k8s.io/kubernetes/pkg/api/unversioned"

type KubeProxyConfiguration struct {
	unversioned.TypeMeta

	// bindAddress is the IP address for the proxy server to serve on (set to 0.0.0.0 for all interfaces)
	BindAddress string `json:"bindAddress"`
	// cleanupIPTables
	CleanupIPTables bool `json:"cleanupIPTables"`
	// healthzBindAddress is the IP address for the health check server to serve on, defaulting to 127.0.0.1 (set to 0.0.0.0 for all interfaces)
	HealthzBindAddress string `json:"healthzBindAddress"`
	// healthzPort is the port to bind the health check server. Use 0 to disable.
	HealthzPort int `json:"healthzPort"`
	// hostnameOverride, if non-empty, will be used as the identity instead of the actual hostname.
	HostnameOverride string `json:"hostnameOverride"`
	// iptablesSyncPeriodSeconds is the period that iptables rules are refreshed (e.g. '5s', '1m', '2h22m').  Must be greater than 0.
	IPTablesSyncePeriodSeconds int `json:"iptablesSyncPeriodSeconds"`
	// kubeAPIBurst is the burst to use while talking with kubernetes apiserver
	KubeAPIBurst int `json:"kubeAPIBurst"`
	// kubeAPIQPS is the max QPS to use while talking with kubernetes apiserver
	KubeAPIQPS int `json:"kubeAPIQPS"`
	// kubeconfigPath is the path to the kubeconfig file with authorization information (the master location is set by the master flag).
	KubeconfigPath string `json:"kubeconfigPath"`
	// masqueradeAll tells kube-proxy to SNAT everything if using the pure iptables proxy mode.
	MasqueradeAll bool `json:"masqueradeAll"`
	// master is the address of the Kubernetes API server (overrides any value in kubeconfig)
	Master string `json:"master"`
	// oomScoreAdj is the oom-score-adj value for kube-proxy process. Values must be within the range [-1000, 1000]
	OOMScoreAdj *int `json:"oomScoreAdj"`
	// mode specifies which proxy mode to use.
	Mode ProxyMode `json:"mode"`
	// portRange is the range of host ports (beginPort-endPort, inclusive) that may be consumed in order to proxy service traffic. If unspecified (0-0) then ports will be randomly chosen.
	PortRange string `json:"portRange"`
	// resourceContainer is the bsolute name of the resource-only container to create and run the Kube-proxy in (Default: /kube-proxy).
	ResourceContainer string `json:"resourceContainer"`
	// udpTimeoutMilliseconds is how long an idle UDP connection will be kept open (e.g. '250ms', '2s').  Must be greater than 0. Only applicable for proxyMode=userspace.
	UDPTimeoutMilliseconds int `json:"udpTimeoutMilliseconds"`
}

// Currently two modes of proxying are available: 'userspace' (older, stable) or 'iptables' (experimental). If blank, look at the Node object on the Kubernetes API and respect the 'net.experimental.kubernetes.io/proxy-mode' annotation if provided.  Otherwise use the best-available proxy (currently userspace, but may change in future versions).  If the iptables proxy is selected, regardless of how, but the system's kernel or iptables versions are insufficient, this always falls back to the userspace proxy.
type ProxyMode string

const (
	ProxyModeUserspace ProxyMode = "userspace"
	ProxyModeIPTables  ProxyMode = "iptables"
)

// LeaderElectionConfiguration defines the configuration of leader election
// clients for components that can run with leader election enabled.
type LeaderElectionConfiguration struct {
	// leaderElect enables a leader election client to gain leadership
	// before executing the main loop. Enable this when running replicated
	// components for high availability.
	LeaderElect bool `json:"leaderElect"`
	// leaseDuration is the duration that non-leader candidates will wait
	// after observing a leadership renewal until attempting to acquire
	// leadership of a led but unrenewed leader slot. This is effectively the
	// maximum duration that a leader can be stopped before it is replaced
	// by another candidate. This is only applicable if leader election is
	// enabled.
	LeaseDuration unversioned.Duration `json:"leaseDuration"`
	// renewDeadline is the interval between attempts by the acting master to
	// renew a leadership slot before it stops leading. This must be less
	// than or equal to the lease duration. This is only applicable if leader
	// election is enabled.
	RenewDeadline unversioned.Duration `json:"renewDeadline"`
	// retryPeriod is the duration the clients should wait between attempting
	// acquisition and renewal of a leadership. This is only applicable if
	// leader election is enabled.
	RetryPeriod unversioned.Duration `json:"retryPeriod"`
}
<commit_msg>kubelet: move most of kubelet server configuration to a config object.<commit_after>/*
Copyright 2015 The Kubernetes Authors All rights reserved.

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 componentconfig

import "k8s.io/kubernetes/pkg/api/unversioned"

type KubeProxyConfiguration struct {
	unversioned.TypeMeta

	// bindAddress is the IP address for the proxy server to serve on (set to 0.0.0.0 for all interfaces)
	BindAddress string `json:"bindAddress"`
	// cleanupIPTables
	CleanupIPTables bool `json:"cleanupIPTables"`
	// healthzBindAddress is the IP address for the health check server to serve on, defaulting to 127.0.0.1 (set to 0.0.0.0 for all interfaces)
	HealthzBindAddress string `json:"healthzBindAddress"`
	// healthzPort is the port to bind the health check server. Use 0 to disable.
	HealthzPort int `json:"healthzPort"`
	// hostnameOverride, if non-empty, will be used as the identity instead of the actual hostname.
	HostnameOverride string `json:"hostnameOverride"`
	// iptablesSyncPeriodSeconds is the period that iptables rules are refreshed (e.g. '5s', '1m', '2h22m').  Must be greater than 0.
	IPTablesSyncePeriodSeconds int `json:"iptablesSyncPeriodSeconds"`
	// kubeAPIBurst is the burst to use while talking with kubernetes apiserver
	KubeAPIBurst int `json:"kubeAPIBurst"`
	// kubeAPIQPS is the max QPS to use while talking with kubernetes apiserver
	KubeAPIQPS int `json:"kubeAPIQPS"`
	// kubeconfigPath is the path to the kubeconfig file with authorization information (the master location is set by the master flag).
	KubeconfigPath string `json:"kubeconfigPath"`
	// masqueradeAll tells kube-proxy to SNAT everything if using the pure iptables proxy mode.
	MasqueradeAll bool `json:"masqueradeAll"`
	// master is the address of the Kubernetes API server (overrides any value in kubeconfig)
	Master string `json:"master"`
	// oomScoreAdj is the oom-score-adj value for kube-proxy process. Values must be within the range [-1000, 1000]
	OOMScoreAdj *int `json:"oomScoreAdj"`
	// mode specifies which proxy mode to use.
	Mode ProxyMode `json:"mode"`
	// portRange is the range of host ports (beginPort-endPort, inclusive) that may be consumed in order to proxy service traffic. If unspecified (0-0) then ports will be randomly chosen.
	PortRange string `json:"portRange"`
	// resourceContainer is the bsolute name of the resource-only container to create and run the Kube-proxy in (Default: /kube-proxy).
	ResourceContainer string `json:"resourceContainer"`
	// udpTimeoutMilliseconds is how long an idle UDP connection will be kept open (e.g. '250ms', '2s').  Must be greater than 0. Only applicable for proxyMode=userspace.
	UDPTimeoutMilliseconds int `json:"udpTimeoutMilliseconds"`
}

// Currently two modes of proxying are available: 'userspace' (older, stable) or 'iptables' (experimental). If blank, look at the Node object on the Kubernetes API and respect the 'net.experimental.kubernetes.io/proxy-mode' annotation if provided.  Otherwise use the best-available proxy (currently userspace, but may change in future versions).  If the iptables proxy is selected, regardless of how, but the system's kernel or iptables versions are insufficient, this always falls back to the userspace proxy.
type ProxyMode string

const (
	ProxyModeUserspace ProxyMode = "userspace"
	ProxyModeIPTables  ProxyMode = "iptables"
)

// TODO: curate the ordering and structure of this config object
type KubeletConfiguration struct {
	// config is the path to the config file or directory of files
	Config string `json:"config"`
	// syncFrequency is the max period between synchronizing running
	// containers and config
	SyncFrequency unversioned.Duration `json:"syncFrequency"`
	// fileCheckFrequency is the duration between checking config files for
	// new data
	FileCheckFrequency unversioned.Duration `json:"fileCheckFrequency"`
	// httpCheckFrequency is the duration between checking http for new data
	HTTPCheckFrequency unversioned.Duration `json:"httpCheckFrequency"`
	// manifestURL is the URL for accessing the container manifest
	ManifestURL string `json:"manifestURL"`
	// manifestURLHeader is the HTTP header to use when accessing the manifest
	// URL, with the key separated from the value with a ':', as in 'key:value'
	ManifestURLHeader string `json:"manifestURLHeader"`
	// enableServer enables the Kubelet's server
	EnableServer bool `json:"enableServer"`
	// address is the IP address for the Kubelet to serve on (set to 0.0.0.0
	// for all interfaces)
	Address string `json:"address"`
	// port is the port for the Kubelet to serve on.
	Port uint `json:"port"`
	// readOnlyPort is the read-only port for the Kubelet to serve on with
	// no authentication/authorization (set to 0 to disable)
	ReadOnlyPort uint `json:"readOnlyPort"`
	// tLSCertFile is the file containing x509 Certificate for HTTPS.  (CA cert,
	// if any, concatenated after server cert). If tlsCertFile and
	// tlsPrivateKeyFile are not provided, a self-signed certificate
	// and key are generated for the public address and saved to the directory
	// passed to certDir.
	TLSCertFile string `json:"tLSCertFile"`
	// tLSPrivateKeyFile is the ile containing x509 private key matching
	// tlsCertFile.
	TLSPrivateKeyFile string `json:"tLSPrivateKeyFile"`
	// certDirectory is the directory where the TLS certs are located (by
	// default /var/run/kubernetes). If tlsCertFile and tlsPrivateKeyFile
	// are provided, this flag will be ignored.
	CertDirectory string `json:"certDirectory"`
	// hostnameOverride is the hostname used to identify the kubelet instead
	// of the actual hostname.
	HostnameOverride string `json:"hostnameOverride"`
	// podInfraContainerImage is the image whose network/ipc namespaces
	// containers in each pod will use.
	PodInfraContainerImage string `json:"podInfraContainerImage"`
	// dockerEndpoint is the path to the docker endpoint to communicate with.
	DockerEndpoint string `json:"dockerEndpoint"`
	// rootDirectory is the directory path to place kubelet files (volume
	// mounts,etc).
	RootDirectory string `json:"rootDirectory"`
	// allowPrivileged enables containers to request privileged mode.
	// Defaults to false.
	AllowPrivileged bool `json:"allowPrivileged"`
	// hostNetworkSources is a comma-separated list of sources from which the
	// Kubelet allows pods to use of host network. Defaults to "*".
	HostNetworkSources string `json:"hostNetworkSources"`
	// hostPIDSources is a comma-separated list of sources from which the
	// Kubelet allows pods to use the host pid namespace. Defaults to "*".
	HostPIDSources string `json:"hostPIDSources"`
	// hostIPCSources is a comma-separated list of sources from which the
	// Kubelet allows pods to use the host ipc namespace. Defaults to "*".
	HostIPCSources string `json:"hostIPCSources"`
	// registryPullQPS is the limit of registry pulls per second. If 0,
	// unlimited. Set to 0 for no limit. Defaults to 5.0.
	RegistryPullQPS float64 `json:"registryPullQPS"`
	// registryBurst is the maximum size of a bursty pulls, temporarily allows
	// pulls to burst to this number, while still not exceeding registryQps.
	// Only used if registryQps > 0.
	RegistryBurst int `json:"registryBurst"`
	// eventRecordQPS is the maximum event creations per second. If 0, there
	// is no limit enforced.
	EventRecordQPS float32 `json:"eventRecordQPS"`
	// eventBurst is the maximum size of a bursty event records, temporarily
	// allows event records to burst to this number, while still not exceeding
	// event-qps. Only used if eventQps > 0
	EventBurst int `json:"eventBurst"`
	// enableDebuggingHandlers enables server endpoints for log collection
	// and local running of containers and commands
	EnableDebuggingHandlers bool `json:"enableDebuggingHandlers"`
	// minimumGCAge is the minimum age for a finished container before it is
	// garbage collected.
	MinimumGCAge unversioned.Duration `json:"minimumGCAge"`
	// maxPerPodContainerCount is the maximum number of old instances to
	// retain per container. Each container takes up some disk space.
	MaxPerPodContainerCount int `json:"maxPerPodContainerCount"`
	// maxContainerCount is the maximum number of old instances of containers
	// to retain globally. Each container takes up some disk space.
	MaxContainerCount int `json:"maxContainerCount"`
	// cAdvisorPort is the port of the localhost cAdvisor endpoint
	CAdvisorPort uint `json:"cAdvisorPort"`
	// healthzPort is the port of the localhost healthz endpoint
	HealthzPort int `json:"healthzPort"`
	// healthzBindAddress is the IP address for the healthz server to serve
	// on.
	HealthzBindAddress string `json:"healthzBindAddress"`
	// oomScoreAdj is The oom-score-adj value for kubelet process. Values
	// must be within the range [-1000, 1000].
	OOMScoreAdj int `json:"oomScoreAdj"`
	// registerNode enables automatic registration with the apiserver.
	RegisterNode bool `json:"registerNode"`
	// clusterDomain is the DNS domain for this cluster. If set, kubelet will
	// configure all containers to search this domain in addition to the
	// host's search domains.
	ClusterDomain string `json:"clusterDomain"`
	// masterServiceNamespace is The namespace from which the kubernetes
	// master services should be injected into pods.
	MasterServiceNamespace string `json:"masterServiceNamespace"`
	// clusterDNS is the IP address for a cluster DNS server.  If set, kubelet
	// will configure all containers to use this for DNS resolution in
	// addition to the host's DNS servers
	ClusterDNS string `json:"clusterDNS"`
	// streamingConnectionIdleTimeout is the maximum time a streaming connection
	// can be idle before the connection is automatically closed.
	StreamingConnectionIdleTimeout unversioned.Duration `json:"streamingConnectionIdleTimeout"`
	// nodeStatusUpdateFrequency is the frequency that kubelet posts node
	// status to master. Note: be cautious when changing the constant, it
	// must work with nodeMonitorGracePeriod in nodecontroller.
	NodeStatusUpdateFrequency unversioned.Duration `json:"nodeStatusUpdateFrequency"`
	// imageGCHighThresholdPercent is the percent of disk usage after which
	// image garbage collection is always run.
	ImageGCHighThresholdPercent int `json:"imageGCHighThresholdPercent"`
	// imageGCLowThresholdPercent is the percent of disk usage before which
	// image garbage collection is never run. Lowest disk usage to garbage
	// collect to.
	ImageGCLowThresholdPercent int `json:"imageGCLowThresholdPercent"`
	// lowDiskSpaceThresholdMB is the absolute free disk space, in MB, to
	// maintain. When disk space falls below this threshold, new pods would
	// be rejected.
	LowDiskSpaceThresholdMB int `json:"lowDiskSpaceThresholdMB"`
	// networkPluginName is the name of the network plugin to be invoked for
	// various events in kubelet/pod lifecycle
	NetworkPluginName string `json:"networkPluginName"`
	// networkPluginDir is the full path of the directory in which to search
	// for network plugins
	NetworkPluginDir string `json:"networkPluginDir"`
	// volumePluginDir is the full path of the directory in which to search
	// for additional third party volume plugins
	VolumePluginDir string `json:"volumePluginDir"`
	// cloudProvider is the provider for cloud services.
	CloudProvider string `json:"cloudProvider,omitempty"`
	// cloudConfigFile is the path to the cloud provider configuration file.
	CloudConfigFile string `json:"cloudConfigFile,omitempty"`
	// resourceContainer is the absolute name of the resource-only container
	// to create and run the Kubelet in.
	ResourceContainer string `json:"resourceContainer,omitempty"`
	// cgroupRoot is the root cgroup to use for pods. This is handled by the
	// container runtime on a best effort basis.
	CgroupRoot string `json:"cgroupRoot,omitempty"`
	// containerRuntime is the container runtime to use.
	ContainerRuntime string `json:"containerRuntime"`
	// rktPath is hte path of rkt binary. Leave empty to use the first rkt in
	// $PATH.
	RktPath string `json:"rktPath,omitempty"`
	// rktStage1Image is the image to use as stage1. Local paths and
	// http/https URLs are supported.
	RktStage1Image string `json:"rktStage1Image,omitempty"`
	// systemContainer is the resource-only container in which to place
	// all non-kernel processes that are not already in a container. Empty
	// for no container. Rolling back the flag requires a reboot.
	SystemContainer string `json:"systemContainer"`
	// configureCBR0 enables the kublet to configure cbr0 based on
	// Node.Spec.PodCIDR.
	ConfigureCBR0 bool `json:"configureCbr0"`
	// maxPods is the number of pods that can run on this Kubelet.
	MaxPods int `json:"maxPods"`
	// dockerExecHandlerName is the handler to use when executing a command
	// in a container. Valid values are 'native' and 'nsenter'. Defaults to
	// 'native'.
	DockerExecHandlerName string `json:"dockerExecHandlerName"`
	// The CIDR to use for pod IP addresses, only used in standalone mode.
	// In cluster mode, this is obtained from the master.
	PodCIDR string `json:"podCIDR"`
	// ResolverConfig is the resolver configuration file used as the basis
	// for the container DNS resolution configuration."), []
	ResolverConfig string `json:"resolvConf"`
	// cpuCFSQuota is Enable CPU CFS quota enforcement for containers that
	// specify CPU limits
	CPUCFSQuota bool `json:"cpuCFSQuota"`
	// containerized should be set to true if kubelet is running in a container.
	Containerized bool `json:"containerized"`
	// maxOpenFiles is Number of files that can be opened by Kubelet process.
	MaxOpenFiles uint64 `json:"maxOpenFiles"`
	// reconcileCIDR is Reconcile node CIDR with the CIDR specified by the
	// API server. No-op if register-node or configure-cbr0 is false.
	ReconcileCIDR bool `json:"reconcileCIDR"`
	// registerSchedulable tells the kubelet to register the node as
	// schedulable. No-op if register-node is false.
	RegisterSchedulable bool `json:"registerSchedulable"`
	// kubeAPIQPS is the QPS to use while talking with kubernetes apiserver
	KubeAPIQPS float32 `json:"kubeAPIQPS"`
	// kubeAPIBurst is the burst to allow while talking with kubernetes
	// apiserver
	KubeAPIBurst int `json:"kubeAPIBurst"`
	// serializeImagePulls when enabled, tells the Kubelet to pull images one
	// at a time. We recommend *not* changing the default value on nodes that
	// run docker daemon with version  < 1.9 or an Aufs storage backend.
	// Issue #10959 has more details.
	SerializeImagePulls bool `json:"serializeImagePulls"`
	// experimentalFlannelOverlay enables experimental support for starting the
	// kubelet with the default overlay network (flannel). Assumes flanneld
	// is already running in client mode.
	ExperimentalFlannelOverlay bool `json:"experimentalFlannelOverlay"`
	// outOfDiskTransitionFrequency is duration for which the kubelet has to
	// wait before transitioning out of out-of-disk node condition status.
	OutOfDiskTransitionFrequency unversioned.Duration `json:"outOfDiskTransitionFrequency,omitempty"`
	// nodeIP is IP address of the node. If set, kubelet will use this IP
	// address for the node.
	NodeIP string `json:"nodeIP,omitempty"`
	// nodeLabels to add when registering the node in the cluster.
	NodeLabels map[string]string `json:"nodeLabels"`
}

// LeaderElectionConfiguration defines the configuration of leader election
// clients for components that can run with leader election enabled.
type LeaderElectionConfiguration struct {
	// leaderElect enables a leader election client to gain leadership
	// before executing the main loop. Enable this when running replicated
	// components for high availability.
	LeaderElect bool `json:"leaderElect"`
	// leaseDuration is the duration that non-leader candidates will wait
	// after observing a leadership renewal until attempting to acquire
	// leadership of a led but unrenewed leader slot. This is effectively the
	// maximum duration that a leader can be stopped before it is replaced
	// by another candidate. This is only applicable if leader election is
	// enabled.
	LeaseDuration unversioned.Duration `json:"leaseDuration"`
	// renewDeadline is the interval between attempts by the acting master to
	// renew a leadership slot before it stops leading. This must be less
	// than or equal to the lease duration. This is only applicable if leader
	// election is enabled.
	RenewDeadline unversioned.Duration `json:"renewDeadline"`
	// retryPeriod is the duration the clients should wait between attempting
	// acquisition and renewal of a leadership. This is only applicable if
	// leader election is enabled.
	RetryPeriod unversioned.Duration `json:"retryPeriod"`
}
<|endoftext|>


<commit_before>package kubernetes

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/proxy"
	pconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy/config"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
	kexec "github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util/iptables"
	dockerclient "github.com/fsouza/go-dockerclient"
	"github.com/golang/glog"

	cmdutil "github.com/openshift/origin/pkg/cmd/util"
	dockerutil "github.com/openshift/origin/pkg/cmd/util/docker"
)

type commandExecutor interface {
	LookPath(executable string) (string, error)
	Run(command string, args ...string) error
}

type defaultCommandExecutor struct{}

func (ce defaultCommandExecutor) LookPath(executable string) (string, error) {
	return exec.LookPath(executable)
}

func (ce defaultCommandExecutor) Run(command string, args ...string) error {
	c := exec.Command(command, args...)
	return c.Run()
}

const minimumDockerAPIVersionWithPullByID = "1.18"

// EnsureDocker attempts to connect to the Docker daemon defined by the helper,
// and if it is unable to it will print a warning.
func (c *NodeConfig) EnsureDocker(docker *dockerutil.Helper) {
	dockerClient, dockerAddr := docker.GetClientOrExit()
	if err := dockerClient.Ping(); err != nil {
		c.HandleDockerError(fmt.Sprintf("Docker could not be reached at %s.  Docker must be installed and running to start containers.\n%v", dockerAddr, err))
		return
	}

	glog.Infof("Connecting to Docker at %s", dockerAddr)

	env, err := dockerClient.Version()
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to check for Docker server version.\n%v", err))
		return
	}

	serverVersionString := env.Get("ApiVersion")
	serverVersion, err := dockerclient.NewAPIVersion(serverVersionString)
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to determine Docker server version from %q.\n%v", serverVersionString, err))
		return
	}

	minimumPullByIDVersion, err := dockerclient.NewAPIVersion(minimumDockerAPIVersionWithPullByID)
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to check for Docker server version.\n%v", err))
		return
	}

	if serverVersion.LessThan(minimumPullByIDVersion) {
		c.HandleDockerError(fmt.Sprintf("Docker 1.6 or later (server API version 1.18 or later) required."))
		return
	}

	c.DockerClient = dockerClient
}

// HandleDockerError handles an an error from the docker daemon
func (c *NodeConfig) HandleDockerError(message string) {
	if !c.AllowDisabledDocker {
		glog.Fatalf("ERROR: %s", message)
	}
	glog.Errorf("WARNING: %s", message)
	c.DockerClient = &dockertools.FakeDockerClient{VersionInfo: dockerclient.Env{"ApiVersion=1.18"}}
}

// EnsureVolumeDir attempts to convert the provided volume directory argument to
// an absolute path and create the directory if it does not exist. Will exit if
// an error is encountered.
func (c *NodeConfig) EnsureVolumeDir() {
	if volumeDir, err := c.initializeVolumeDir(&defaultCommandExecutor{}, c.VolumeDir); err != nil {
		glog.Fatal(err)
	} else {
		c.VolumeDir = volumeDir
	}
}

func (c *NodeConfig) initializeVolumeDir(ce commandExecutor, path string) (string, error) {
	rootDirectory, err := filepath.Abs(path)
	if err != nil {
		return "", fmt.Errorf("Error converting volume directory to an absolute path: %v", err)
	}

	if _, err := os.Stat(rootDirectory); os.IsNotExist(err) {
		if err := os.MkdirAll(rootDirectory, 0750); err != nil {
			return "", fmt.Errorf("Couldn't create kubelet volume root directory '%s': %s", rootDirectory, err)
		}
		if chconPath, err := ce.LookPath("chcon"); err != nil {
			glog.V(2).Infof("Couldn't locate 'chcon' to set the kubelet volume root directory SELinux context: %s", err)
		} else {
			if err := ce.Run(chconPath, "-t", "svirt_sandbox_file_t", rootDirectory); err != nil {
				glog.Warningf("Error running 'chcon' to set the kubelet volume root directory SELinux context: %s", err)
			}
		}
	}
	return rootDirectory, nil
}

// RunKubelet starts the Kubelet.
func (c *NodeConfig) RunKubelet() {
	// TODO: clean this up and make it more formal (service named 'dns'?). Use multiple ports.
	clusterDNS := c.KubeletConfig.ClusterDNS
	if clusterDNS == nil {
		if service, err := c.Client.Endpoints(kapi.NamespaceDefault).Get("kubernetes"); err == nil {
			if ip, ok := firstIP(service, 53); ok {
				if err := cmdutil.WaitForSuccessfulDial(false, "tcp", fmt.Sprintf("%s:%d", ip, 53), 50*time.Millisecond, 0, 2); err == nil {
					c.KubeletConfig.ClusterDNS = util.IP(net.ParseIP(ip))
				}
			}
		}
	}
	c.KubeletConfig.DockerClient = c.DockerClient
	glog.Fatal(c.KubeletServer.Run(c.KubeletConfig))
}

// RunProxy starts the proxy
func (c *NodeConfig) RunProxy() {
	// initialize kube proxy
	serviceConfig := pconfig.NewServiceConfig()
	endpointsConfig := pconfig.NewEndpointsConfig()
	loadBalancer := proxy.NewLoadBalancerRR()
	endpointsConfig.RegisterHandler(loadBalancer)

	host, _, err := net.SplitHostPort(c.BindAddress)
	if err != nil {
		glog.Fatalf("The provided value to bind to must be an ip:port %q", c.BindAddress)
	}
	ip := net.ParseIP(host)
	if ip == nil {
		glog.Fatalf("The provided value to bind to must be an ip:port: %q", c.BindAddress)
	}

	protocol := iptables.ProtocolIpv4
	if ip.To4() == nil {
		protocol = iptables.ProtocolIpv6
	}

	go util.Forever(func() {
		proxier, err := proxy.NewProxier(loadBalancer, ip, iptables.New(kexec.New(), protocol))
		if err != nil {
			switch {
			// conflicting use of iptables, retry
			case proxy.IsProxyLocked(err):
				glog.Errorf("Unable to start proxy, will retry: %v", err)
				return
			// on a system without iptables
			case strings.Contains(err.Error(), "executable file not found in path"):
				glog.V(4).Infof("kube-proxy initialization error: %v", err)
				glog.Warningf("WARNING: Could not find the iptables command. The service proxy requires iptables and will be disabled.")
			case err == proxy.ErrProxyOnLocalhost:
				glog.Warningf("WARNING: The service proxy cannot bind to localhost and will be disabled.")
			case strings.Contains(err.Error(), "you must be root"):
				glog.Warningf("WARNING: Could not modify iptables. You must run this process as root to use the service proxy.")
			default:
				glog.Warningf("WARNING: Could not modify iptables. You must run this process as root to use the service proxy: %v", err)
			}
			select {}
		}

		pconfig.NewSourceAPI(
			c.Client.Services(kapi.NamespaceAll),
			c.Client.Endpoints(kapi.NamespaceAll),
			30*time.Second,
			serviceConfig.Channel("api"),
			endpointsConfig.Channel("api"))

		serviceConfig.RegisterHandler(proxier)
		glog.Infof("Started Kubernetes Proxy on %s", host)
		select {}
	}, 5*time.Second)
}

// TODO: more generic location
func includesPort(ports []kapi.EndpointPort, port int) bool {
	for _, p := range ports {
		if p.Port == port {
			return true
		}
	}
	return false
}

// TODO: more generic location
func firstIP(endpoints *kapi.Endpoints, port int) (string, bool) {
	for _, s := range endpoints.Subsets {
		if !includesPort(s.Ports, port) {
			continue
		}
		for _, a := range s.Addresses {
			return a.IP, true
		}
	}
	return "", false
}
<commit_msg>Ensure KubeletConfig's RootDirectory is correct<commit_after>package kubernetes

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/proxy"
	pconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy/config"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
	kexec "github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util/iptables"
	dockerclient "github.com/fsouza/go-dockerclient"
	"github.com/golang/glog"

	cmdutil "github.com/openshift/origin/pkg/cmd/util"
	dockerutil "github.com/openshift/origin/pkg/cmd/util/docker"
)

type commandExecutor interface {
	LookPath(executable string) (string, error)
	Run(command string, args ...string) error
}

type defaultCommandExecutor struct{}

func (ce defaultCommandExecutor) LookPath(executable string) (string, error) {
	return exec.LookPath(executable)
}

func (ce defaultCommandExecutor) Run(command string, args ...string) error {
	c := exec.Command(command, args...)
	return c.Run()
}

const minimumDockerAPIVersionWithPullByID = "1.18"

// EnsureDocker attempts to connect to the Docker daemon defined by the helper,
// and if it is unable to it will print a warning.
func (c *NodeConfig) EnsureDocker(docker *dockerutil.Helper) {
	dockerClient, dockerAddr := docker.GetClientOrExit()
	if err := dockerClient.Ping(); err != nil {
		c.HandleDockerError(fmt.Sprintf("Docker could not be reached at %s.  Docker must be installed and running to start containers.\n%v", dockerAddr, err))
		return
	}

	glog.Infof("Connecting to Docker at %s", dockerAddr)

	env, err := dockerClient.Version()
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to check for Docker server version.\n%v", err))
		return
	}

	serverVersionString := env.Get("ApiVersion")
	serverVersion, err := dockerclient.NewAPIVersion(serverVersionString)
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to determine Docker server version from %q.\n%v", serverVersionString, err))
		return
	}

	minimumPullByIDVersion, err := dockerclient.NewAPIVersion(minimumDockerAPIVersionWithPullByID)
	if err != nil {
		c.HandleDockerError(fmt.Sprintf("Unable to check for Docker server version.\n%v", err))
		return
	}

	if serverVersion.LessThan(minimumPullByIDVersion) {
		c.HandleDockerError(fmt.Sprintf("Docker 1.6 or later (server API version 1.18 or later) required."))
		return
	}

	c.DockerClient = dockerClient
}

// HandleDockerError handles an an error from the docker daemon
func (c *NodeConfig) HandleDockerError(message string) {
	if !c.AllowDisabledDocker {
		glog.Fatalf("ERROR: %s", message)
	}
	glog.Errorf("WARNING: %s", message)
	c.DockerClient = &dockertools.FakeDockerClient{VersionInfo: dockerclient.Env{"ApiVersion=1.18"}}
}

// EnsureVolumeDir attempts to convert the provided volume directory argument to
// an absolute path and create the directory if it does not exist. Will exit if
// an error is encountered.
func (c *NodeConfig) EnsureVolumeDir() {
	if volumeDir, err := c.initializeVolumeDir(&defaultCommandExecutor{}, c.VolumeDir); err != nil {
		glog.Fatal(err)
	} else {
		c.VolumeDir = volumeDir
	}
}

func (c *NodeConfig) initializeVolumeDir(ce commandExecutor, path string) (string, error) {
	rootDirectory, err := filepath.Abs(path)
	if err != nil {
		return "", fmt.Errorf("Error converting volume directory to an absolute path: %v", err)
	}

	if _, err := os.Stat(rootDirectory); os.IsNotExist(err) {
		if err := os.MkdirAll(rootDirectory, 0750); err != nil {
			return "", fmt.Errorf("Couldn't create kubelet volume root directory '%s': %s", rootDirectory, err)
		}
		if chconPath, err := ce.LookPath("chcon"); err != nil {
			glog.V(2).Infof("Couldn't locate 'chcon' to set the kubelet volume root directory SELinux context: %s", err)
		} else {
			if err := ce.Run(chconPath, "-t", "svirt_sandbox_file_t", rootDirectory); err != nil {
				glog.Warningf("Error running 'chcon' to set the kubelet volume root directory SELinux context: %s", err)
			}
		}
	}
	return rootDirectory, nil
}

// RunKubelet starts the Kubelet.
func (c *NodeConfig) RunKubelet() {
	// TODO: clean this up and make it more formal (service named 'dns'?). Use multiple ports.
	clusterDNS := c.KubeletConfig.ClusterDNS
	if clusterDNS == nil {
		if service, err := c.Client.Endpoints(kapi.NamespaceDefault).Get("kubernetes"); err == nil {
			if ip, ok := firstIP(service, 53); ok {
				if err := cmdutil.WaitForSuccessfulDial(false, "tcp", fmt.Sprintf("%s:%d", ip, 53), 50*time.Millisecond, 0, 2); err == nil {
					c.KubeletConfig.ClusterDNS = util.IP(net.ParseIP(ip))
				}
			}
		}
	}
	c.KubeletConfig.DockerClient = c.DockerClient
	// updated by NodeConfig.EnsureVolumeDir
	c.KubeletConfig.RootDirectory = c.VolumeDir
	glog.Fatal(c.KubeletServer.Run(c.KubeletConfig))
}

// RunProxy starts the proxy
func (c *NodeConfig) RunProxy() {
	// initialize kube proxy
	serviceConfig := pconfig.NewServiceConfig()
	endpointsConfig := pconfig.NewEndpointsConfig()
	loadBalancer := proxy.NewLoadBalancerRR()
	endpointsConfig.RegisterHandler(loadBalancer)

	host, _, err := net.SplitHostPort(c.BindAddress)
	if err != nil {
		glog.Fatalf("The provided value to bind to must be an ip:port %q", c.BindAddress)
	}
	ip := net.ParseIP(host)
	if ip == nil {
		glog.Fatalf("The provided value to bind to must be an ip:port: %q", c.BindAddress)
	}

	protocol := iptables.ProtocolIpv4
	if ip.To4() == nil {
		protocol = iptables.ProtocolIpv6
	}

	go util.Forever(func() {
		proxier, err := proxy.NewProxier(loadBalancer, ip, iptables.New(kexec.New(), protocol))
		if err != nil {
			switch {
			// conflicting use of iptables, retry
			case proxy.IsProxyLocked(err):
				glog.Errorf("Unable to start proxy, will retry: %v", err)
				return
			// on a system without iptables
			case strings.Contains(err.Error(), "executable file not found in path"):
				glog.V(4).Infof("kube-proxy initialization error: %v", err)
				glog.Warningf("WARNING: Could not find the iptables command. The service proxy requires iptables and will be disabled.")
			case err == proxy.ErrProxyOnLocalhost:
				glog.Warningf("WARNING: The service proxy cannot bind to localhost and will be disabled.")
			case strings.Contains(err.Error(), "you must be root"):
				glog.Warningf("WARNING: Could not modify iptables. You must run this process as root to use the service proxy.")
			default:
				glog.Warningf("WARNING: Could not modify iptables. You must run this process as root to use the service proxy: %v", err)
			}
			select {}
		}

		pconfig.NewSourceAPI(
			c.Client.Services(kapi.NamespaceAll),
			c.Client.Endpoints(kapi.NamespaceAll),
			30*time.Second,
			serviceConfig.Channel("api"),
			endpointsConfig.Channel("api"))

		serviceConfig.RegisterHandler(proxier)
		glog.Infof("Started Kubernetes Proxy on %s", host)
		select {}
	}, 5*time.Second)
}

// TODO: more generic location
func includesPort(ports []kapi.EndpointPort, port int) bool {
	for _, p := range ports {
		if p.Port == port {
			return true
		}
	}
	return false
}

// TODO: more generic location
func firstIP(endpoints *kapi.Endpoints, port int) (string, bool) {
	for _, s := range endpoints.Subsets {
		if !includesPort(s.Ports, port) {
			continue
		}
		for _, a := range s.Addresses {
			return a.IP, true
		}
	}
	return "", false
}
<|endoftext|>


<commit_before>package controller_test

import (
	"fmt"

	. "github.com/kubevirt/containerized-data-importer/pkg/controller"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
	"k8s.io/client-go/tools/cache"
	k8stesting "k8s.io/client-go/tools/cache/testing"
	"k8s.io/client-go/util/workqueue"
)

type operation int

const (
	opAdd operation = iota
	opUpdate
	opDelete
)

var _ = Describe("Controller", func() {
	const pvcName = "test-pvc"
	var (
		controller *Controller
		fakeClient *fake.Clientset
		pvcObj     *v1.PersistentVolumeClaim
		stop       chan struct{}
	)
	type testT struct {
		descr         string
		ns            string
		annEndpoint   string
		expectPodName string
		expectError   bool
	}

	setUpInformer := func(obj *v1.PersistentVolumeClaim, op operation, pvcName string) {
		stop = make(chan struct{})
		fakeClient = fake.NewSimpleClientset()
		importerTag := "latest"
		objSource := k8stesting.NewFakeControllerSource()
		pvcInformer := cache.NewSharedIndexInformer(objSource, obj, 0, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
		pvcListWatcher := k8stesting.NewFakeControllerSource()
		controller = NewController(fakeClient, queue, pvcInformer, pvcListWatcher, importerTag)
		if op == opAdd {
			pvcListWatcher.Add(obj)
			objSource.Add(obj)
			queue.Add(pvcName)
		}
		go pvcInformer.Run(stop)
		Expect(cache.WaitForCacheSync(stop, pvcInformer.HasSynced)).To(BeTrue())
	}

	BeforeEach(func() {
		// anno and namespace may be updated in It block
		pvcObj = &v1.PersistentVolumeClaim{
			ObjectMeta: metav1.ObjectMeta{
				Name:        pvcName,
				Namespace:   "",
				Annotations: map[string]string{AnnEndpoint: ""},
			},
		}
	})

	AfterEach(func() {
		close(stop)
	})

	tests := []testT{
		{
			descr:         "pvc, endpoint, blank ns: controller creates importer pod",
			ns:            "", // seems to be default for unit tests
			annEndpoint:   "http://www.google.com",
			expectPodName: "importer-" + pvcName,
			expectError:   false,
		},
		{
			descr:         "pvc, endpoint, non-blank ns: controller creates importer pod",
			ns:            "ns-a",
			annEndpoint:   "http://www.google.com",
			expectPodName: "importer-" + pvcName,
			expectError:   false,
		},
		{
			descr:         "pvc, blank endpoint: controller does not create importer pod",
			ns:            "",
			annEndpoint:   "",
			expectPodName: "",
			expectError:   true,
		},
	}

	for _, test := range tests {
		ep := test.annEndpoint
		ns := test.ns
		exptPod := test.expectPodName
		exptErr := test.expectError
		It(test.descr, func() {
			By(fmt.Sprintf("setting the pvc's endpt anno=%q and ns=%q", ep, ns))
			pvcObj.Annotations[AnnEndpoint] = ep
			pvcObj.Namespace = ns
			By("invoking the controller")
			setUpInformer(pvcObj, opAdd, pvcName)
			controller.ProcessNextItem()
			By("checking if importer pod is present")
			pod, err := getImporterPod(fakeClient, ns, exptPod)
			if exptErr {
				Expect(err).ToNot(BeNil(), fmt.Sprintf("importer pod %s... should not exist\n", exptPod))
			} else {
				Expect(err).To(BeNil(), fmt.Sprintf("importer pod: %v\n", err))
				Expect(pod).ToNot(BeNil(), fmt.Sprintf("importer pod %q missing", exptPod))
				Expect(pod.GenerateName).To(HavePrefix(exptPod))
			}
		})
	}
})

// getImporterPod gets the first pod with a generated name equal to the passed-in name.
// Nil is returned if no match is found.
func getImporterPod(fc *fake.Clientset, ns, podName string) (*v1.Pod, error) {
	podList, err := fc.CoreV1().Pods(ns).List(metav1.ListOptions{})
	if err != nil {
		return nil, fmt.Errorf("getImporterPod: %v\n", err)
	}
	if len(podList.Items) == 0 {
		return nil, fmt.Errorf("getImporterPod: no pods found in namespace %q\n", ns)
	}
	for i, p := range podList.Items {
		if p.GenerateName == podName {
			return &podList.Items[i], nil
		}
	}
	return nil, fmt.Errorf("getImporterPod: no pods match %s/%s\n", ns, podName)
}
<commit_msg>fix controller unit test to be namespace aware<commit_after>package controller_test

import (
	"fmt"

	. "github.com/kubevirt/containerized-data-importer/pkg/controller"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
	"k8s.io/client-go/tools/cache"
	k8stesting "k8s.io/client-go/tools/cache/testing"
	"k8s.io/client-go/util/workqueue"
)

type operation int

const (
	opAdd operation = iota
	opUpdate
	opDelete
)

var _ = Describe("Controller", func() {
	const pvcName = "test-pvc"
	var (
		controller *Controller
		fakeClient *fake.Clientset
		pvcObj     *v1.PersistentVolumeClaim
		stop       chan struct{}
	)
	type testT struct {
		descr         string
		ns            string
		annEndpoint   string
		expectPodName string
		expectError   bool
	}

	setUpInformer := func(obj *v1.PersistentVolumeClaim, op operation, ns string, pvcName string) {
		// build queue value of ns + "/" + pvcName if exists
		queueKey := pvcName
		if len(ns) > 0 {
			queueKey = fmt.Sprintf("%s/%s", ns, pvcName)
		}

		stop = make(chan struct{})
		fakeClient = fake.NewSimpleClientset()
		importerTag := "latest"
		objSource := k8stesting.NewFakeControllerSource()
		pvcInformer := cache.NewSharedIndexInformer(objSource, obj, 0, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
		pvcListWatcher := k8stesting.NewFakeControllerSource()
		controller = NewController(fakeClient, queue, pvcInformer, pvcListWatcher, importerTag)
		if op == opAdd {
			pvcListWatcher.Add(obj)
			objSource.Add(obj)
			queue.Add(queueKey)
		}
		go pvcInformer.Run(stop)
		Expect(cache.WaitForCacheSync(stop, pvcInformer.HasSynced)).To(BeTrue())
	}

	BeforeEach(func() {
		// anno and namespace may be updated in It block
		pvcObj = &v1.PersistentVolumeClaim{
			ObjectMeta: metav1.ObjectMeta{
				Name:        pvcName,
				Namespace:   "",
				Annotations: map[string]string{AnnEndpoint: ""},
			},
		}
	})

	AfterEach(func() {
		close(stop)
	})

	tests := []testT{
		{
			descr:         "pvc, endpoint, blank ns: controller creates importer pod",
			ns:            "", // seems to be default for unit tests
			annEndpoint:   "http://www.google.com",
			expectPodName: "importer-" + pvcName,
			expectError:   false,
		},
		{
			descr:         "pvc, endpoint, non-blank ns: controller creates importer pod",
			ns:            "ns-a",
			annEndpoint:   "http://www.google.com",
			expectPodName: "importer-" + pvcName,
			expectError:   false,
		},
		{
			descr:         "pvc, blank endpoint: controller does not create importer pod",
			ns:            "",
			annEndpoint:   "",
			expectPodName: "",
			expectError:   true,
		},
	}

	for _, test := range tests {
		ep := test.annEndpoint
		ns := test.ns
		exptPod := test.expectPodName
		exptErr := test.expectError
		It(test.descr, func() {
			By(fmt.Sprintf("setting the pvc's endpt anno=%q and ns=%q", ep, ns))
			pvcObj.Annotations[AnnEndpoint] = ep
			pvcObj.Namespace = ns
			By("invoking the controller")
			setUpInformer(pvcObj, opAdd, ns, pvcName)
			controller.ProcessNextItem()
			By("checking if importer pod is present")
			pod, err := getImporterPod(fakeClient, ns, exptPod)
			if exptErr {
				Expect(err).ToNot(BeNil(), fmt.Sprintf("importer pod %s... should not exist\n", exptPod))
			} else {
				Expect(err).To(BeNil(), fmt.Sprintf("importer pod: %v\n", err))
				Expect(pod).ToNot(BeNil(), fmt.Sprintf("importer pod %q missing", exptPod))
				Expect(pod.GenerateName).To(HavePrefix(exptPod))
			}
		})
	}
})

// getImporterPod gets the first pod with a generated name equal to the passed-in name.
// Nil is returned if no match is found.
func getImporterPod(fc *fake.Clientset, ns, podName string) (*v1.Pod, error) {
	podList, err := fc.CoreV1().Pods(ns).List(metav1.ListOptions{})
	if err != nil {
		return nil, fmt.Errorf("getImporterPod: %v\n", err)
	}
	if len(podList.Items) == 0 {
		return nil, fmt.Errorf("getImporterPod: no pods found in namespace %q\n", ns)
	}
	for i, p := range podList.Items {
		if p.GenerateName == podName {
			return &podList.Items[i], nil
		}
	}
	return nil, fmt.Errorf("getImporterPod: no pods match %s/%s\n", ns, podName)
}
<|endoftext|>


<commit_before>/*
Copyright 2018 Mirantis

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 types

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/ghodss/yaml"
	libvirtxml "github.com/libvirt/libvirt-go-xml"

	"github.com/Mirantis/virtlet/pkg/utils"
)

const (
	maxVCPUCount                      = 255
	vcpuCountAnnotationKeyName        = "VirtletVCPUCount"
	diskDriverKeyName                 = "VirtletDiskDriver"
	cloudInitMetaDataKeyName          = "VirtletCloudInitMetaData"
	cloudInitUserDataOverwriteKeyName = "VirtletCloudInitUserDataOverwrite"
	cloudInitUserDataKeyName          = "VirtletCloudInitUserData"
	cloudInitUserDataScriptKeyName    = "VirtletCloudInitUserDataScript"
	cloudInitImageType                = "VirtletCloudInitImageType"
	cpuModel                          = "VirtletCPUModel"
	libvirtCPUSetting                 = "VirtletLibvirtCPUSetting"
	sshKeysKeyName                    = "VirtletSSHKeys"
	// CloudInitUserDataSourceKeyName is the name of user data source key in the pod annotations.
	CloudInitUserDataSourceKeyName = "VirtletCloudInitUserDataSource"
	// SSHKeySourceKeyName is the name of ssh key source key in the pod annotations.
	SSHKeySourceKeyName = "VirtletSSHKeySource"
)

// CloudInitImageType specifies the image type used for cloud-init
type CloudInitImageType string

// CPUModelType specifies cpu model in libvirt domain definition
type CPUModelType string

const (
	// CloudInitImageTypeNoCloud specified nocloud cloud-init image type.
	CloudInitImageTypeNoCloud CloudInitImageType = "nocloud"
	// CloudInitImageTypeConfigDrive specified configdrive cloud-init image type.
	CloudInitImageTypeConfigDrive CloudInitImageType = "configdrive"
	// CPUModelHostModel specifies cpu model needed for nested virtualization
	CPUModelHostModel = "host-model"
)

// DiskDriverName specifies disk driver name supported by Virtlet.
type DiskDriverName string

const (
	// DiskDriverVirtio specifies virtio disk driver.
	DiskDriverVirtio DiskDriverName = "virtio"
	// DiskDriverScsi specifies scsi disk driver.
	DiskDriverScsi DiskDriverName = "scsi"
)

// VirtletAnnotations contains parsed values for pod annotations supported
// by Virtlet.
type VirtletAnnotations struct {
	// Number of virtual CPUs.
	VCPUCount int
	// CPU model.
	CPUModel CPUModelType
	// Cloud-Init image type to use.
	CDImageType CloudInitImageType
	// Cloud-Init metadata.
	MetaData map[string]interface{}
	// Cloud-Init userdata
	UserData map[string]interface{}
	// True if the userdata is overridden.
	UserDataOverwrite bool
	// UserDataScript specifies the script to be used as userdata.
	UserDataScript string
	// SSHKets specifies ssh public keys to use.
	SSHKeys []string
	// DiskDriver specifies the disk driver to use.
	DiskDriver DiskDriverName
	// CPUSetting directly specifies the cpu to use for libvirt.
	CPUSetting *libvirtxml.DomainCPU
}

// ExternalDataLoader is a function that loads external data that's specified
// in the pod annotations.
type ExternalDataLoader func(va *VirtletAnnotations, Namespace string, podAnnotations map[string]string) error

var externalDataLoader ExternalDataLoader

// SetExternalDataLoader sets the external data loader function that
// loads external data that's specified in the pod annotations.
func SetExternalDataLoader(loader ExternalDataLoader) {
	externalDataLoader = loader
}

func (va *VirtletAnnotations) applyDefaults() {
	if va.VCPUCount <= 0 {
		va.VCPUCount = 1
	}

	if va.DiskDriver == "" {
		va.DiskDriver = DiskDriverScsi
	}

	if va.CDImageType == "" {
		va.CDImageType = CloudInitImageTypeNoCloud
	}
}

func (va *VirtletAnnotations) validate() error {
	var errs []string
	if va.VCPUCount > maxVCPUCount {
		errs = append(errs, fmt.Sprintf("vcpu count %d too big, max is %d", va.VCPUCount, maxVCPUCount))
	}

	if va.DiskDriver != DiskDriverVirtio && va.DiskDriver != DiskDriverScsi {
		errs = append(errs, fmt.Sprintf("bad disk driver %q. Must be either %q or %q", va.DiskDriver, DiskDriverVirtio, DiskDriverScsi))
	}

	if va.CDImageType != CloudInitImageTypeNoCloud && va.CDImageType != CloudInitImageTypeConfigDrive {
		errs = append(errs, fmt.Sprintf("unknown config image type %q. Must be either %q or %q", va.CDImageType, CloudInitImageTypeNoCloud, CloudInitImageTypeConfigDrive))
	}

	if va.CPUModel != "" && va.CPUModel != CPUModelHostModel {
		errs = append(errs, fmt.Sprintf("unknown cpu model type %q. Must be empty or %q", va.CPUModel, CPUModelHostModel))
	}

	if errs != nil {
		return fmt.Errorf("bad virtlet annotations. Errors:\n%s", strings.Join(errs, "\n"))
	}

	return nil
}

func loadAnnotations(ns string, podAnnotations map[string]string) (*VirtletAnnotations, error) {
	var va VirtletAnnotations
	if err := va.parsePodAnnotations(ns, podAnnotations); err != nil {
		return nil, err
	}
	va.applyDefaults()
	if err := va.validate(); err != nil {
		return nil, err
	}
	return &va, nil
}

func (va *VirtletAnnotations) parsePodAnnotations(ns string, podAnnotations map[string]string) error {
	if cpuSettingStr, found := podAnnotations[libvirtCPUSetting]; found {
		var cpuSetting libvirtxml.DomainCPU
		if err := yaml.Unmarshal([]byte(cpuSettingStr), &cpuSetting); err != nil {
			return err
		}
		va.CPUSetting = &cpuSetting
	}

	if podAnnotations[cloudInitUserDataOverwriteKeyName] == "true" {
		va.UserDataOverwrite = true
	}
	if externalDataLoader != nil {
		if err := externalDataLoader(va, ns, podAnnotations); err != nil {
			return fmt.Errorf("error loading data via external data loader: %v", err)
		}
	}

	if vcpuCountStr, found := podAnnotations[vcpuCountAnnotationKeyName]; found {
		var err error
		if va.VCPUCount, err = strconv.Atoi(vcpuCountStr); err != nil {
			return fmt.Errorf("error parsing cpu count for VM pod %q: %v", vcpuCountStr, err)
		}
	}

	if metaDataStr, found := podAnnotations[cloudInitMetaDataKeyName]; found {
		if err := yaml.Unmarshal([]byte(metaDataStr), &va.MetaData); err != nil {
			return fmt.Errorf("failed to unmarshal cloud-init metadata: %v", err)
		}
	}

	if userDataStr, found := podAnnotations[cloudInitUserDataKeyName]; found {
		var userData map[string]interface{}
		if err := yaml.Unmarshal([]byte(userDataStr), &userData); err != nil {
			return fmt.Errorf("failed to unmarshal cloud-init userdata: %v", err)
		}
		if va.UserDataOverwrite {
			va.UserData = userData
		} else {
			va.UserData = utils.Merge(va.UserData, userData).(map[string]interface{})
		}
	}

	va.UserDataScript = podAnnotations[cloudInitUserDataScriptKeyName]

	if sshKeysStr, found := podAnnotations[sshKeysKeyName]; found {
		if va.UserDataOverwrite {
			va.SSHKeys = nil
		}
		keys := strings.Split(sshKeysStr, "\n")
		for _, k := range keys {
			k = strings.TrimSpace(k)
			if k != "" {
				va.SSHKeys = append(va.SSHKeys, k)
			}
		}
	}

	va.CDImageType = CloudInitImageType(strings.ToLower(podAnnotations[cloudInitImageType]))
	va.DiskDriver = DiskDriverName(podAnnotations[diskDriverKeyName])

	return nil
}
<commit_msg>need to parse VirtletCPUModel in annotation<commit_after>/*
Copyright 2018 Mirantis

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 types

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/ghodss/yaml"
	libvirtxml "github.com/libvirt/libvirt-go-xml"

	"github.com/Mirantis/virtlet/pkg/utils"
)

const (
	maxVCPUCount                      = 255
	vcpuCountAnnotationKeyName        = "VirtletVCPUCount"
	diskDriverKeyName                 = "VirtletDiskDriver"
	cloudInitMetaDataKeyName          = "VirtletCloudInitMetaData"
	cloudInitUserDataOverwriteKeyName = "VirtletCloudInitUserDataOverwrite"
	cloudInitUserDataKeyName          = "VirtletCloudInitUserData"
	cloudInitUserDataScriptKeyName    = "VirtletCloudInitUserDataScript"
	cloudInitImageType                = "VirtletCloudInitImageType"
	cpuModel                          = "VirtletCPUModel"
	libvirtCPUSetting                 = "VirtletLibvirtCPUSetting"
	sshKeysKeyName                    = "VirtletSSHKeys"
	// CloudInitUserDataSourceKeyName is the name of user data source key in the pod annotations.
	CloudInitUserDataSourceKeyName = "VirtletCloudInitUserDataSource"
	// SSHKeySourceKeyName is the name of ssh key source key in the pod annotations.
	SSHKeySourceKeyName = "VirtletSSHKeySource"
)

// CloudInitImageType specifies the image type used for cloud-init
type CloudInitImageType string

// CPUModelType specifies cpu model in libvirt domain definition
type CPUModelType string

const (
	// CloudInitImageTypeNoCloud specified nocloud cloud-init image type.
	CloudInitImageTypeNoCloud CloudInitImageType = "nocloud"
	// CloudInitImageTypeConfigDrive specified configdrive cloud-init image type.
	CloudInitImageTypeConfigDrive CloudInitImageType = "configdrive"
	// CPUModelHostModel specifies cpu model needed for nested virtualization
	CPUModelHostModel = "host-model"
)

// DiskDriverName specifies disk driver name supported by Virtlet.
type DiskDriverName string

const (
	// DiskDriverVirtio specifies virtio disk driver.
	DiskDriverVirtio DiskDriverName = "virtio"
	// DiskDriverScsi specifies scsi disk driver.
	DiskDriverScsi DiskDriverName = "scsi"
)

// VirtletAnnotations contains parsed values for pod annotations supported
// by Virtlet.
type VirtletAnnotations struct {
	// Number of virtual CPUs.
	VCPUCount int
	// CPU model.
	CPUModel CPUModelType
	// Cloud-Init image type to use.
	CDImageType CloudInitImageType
	// Cloud-Init metadata.
	MetaData map[string]interface{}
	// Cloud-Init userdata
	UserData map[string]interface{}
	// True if the userdata is overridden.
	UserDataOverwrite bool
	// UserDataScript specifies the script to be used as userdata.
	UserDataScript string
	// SSHKets specifies ssh public keys to use.
	SSHKeys []string
	// DiskDriver specifies the disk driver to use.
	DiskDriver DiskDriverName
	// CPUSetting directly specifies the cpu to use for libvirt.
	CPUSetting *libvirtxml.DomainCPU
}

// ExternalDataLoader is a function that loads external data that's specified
// in the pod annotations.
type ExternalDataLoader func(va *VirtletAnnotations, Namespace string, podAnnotations map[string]string) error

var externalDataLoader ExternalDataLoader

// SetExternalDataLoader sets the external data loader function that
// loads external data that's specified in the pod annotations.
func SetExternalDataLoader(loader ExternalDataLoader) {
	externalDataLoader = loader
}

func (va *VirtletAnnotations) applyDefaults() {
	if va.VCPUCount <= 0 {
		va.VCPUCount = 1
	}

	if va.DiskDriver == "" {
		va.DiskDriver = DiskDriverScsi
	}

	if va.CDImageType == "" {
		va.CDImageType = CloudInitImageTypeNoCloud
	}
}

func (va *VirtletAnnotations) validate() error {
	var errs []string
	if va.VCPUCount > maxVCPUCount {
		errs = append(errs, fmt.Sprintf("vcpu count %d too big, max is %d", va.VCPUCount, maxVCPUCount))
	}

	if va.DiskDriver != DiskDriverVirtio && va.DiskDriver != DiskDriverScsi {
		errs = append(errs, fmt.Sprintf("bad disk driver %q. Must be either %q or %q", va.DiskDriver, DiskDriverVirtio, DiskDriverScsi))
	}

	if va.CDImageType != CloudInitImageTypeNoCloud && va.CDImageType != CloudInitImageTypeConfigDrive {
		errs = append(errs, fmt.Sprintf("unknown config image type %q. Must be either %q or %q", va.CDImageType, CloudInitImageTypeNoCloud, CloudInitImageTypeConfigDrive))
	}

	if va.CPUModel != "" && va.CPUModel != CPUModelHostModel {
		errs = append(errs, fmt.Sprintf("unknown cpu model type %q. Must be empty or %q", va.CPUModel, CPUModelHostModel))
	}

	if errs != nil {
		return fmt.Errorf("bad virtlet annotations. Errors:\n%s", strings.Join(errs, "\n"))
	}

	return nil
}

func loadAnnotations(ns string, podAnnotations map[string]string) (*VirtletAnnotations, error) {
	var va VirtletAnnotations
	if err := va.parsePodAnnotations(ns, podAnnotations); err != nil {
		return nil, err
	}
	va.applyDefaults()
	if err := va.validate(); err != nil {
		return nil, err
	}
	return &va, nil
}

func (va *VirtletAnnotations) parsePodAnnotations(ns string, podAnnotations map[string]string) error {
	if cpuSettingStr, found := podAnnotations[libvirtCPUSetting]; found {
		var cpuSetting libvirtxml.DomainCPU
		if err := yaml.Unmarshal([]byte(cpuSettingStr), &cpuSetting); err != nil {
			return err
		}
		va.CPUSetting = &cpuSetting
	}

	if cpuModelStr, found := podAnnotations[cpuModel]; found {
		va.CPUModel = CPUModelType(cpuModelStr)
	}

	if podAnnotations[cloudInitUserDataOverwriteKeyName] == "true" {
		va.UserDataOverwrite = true
	}
	if externalDataLoader != nil {
		if err := externalDataLoader(va, ns, podAnnotations); err != nil {
			return fmt.Errorf("error loading data via external data loader: %v", err)
		}
	}

	if vcpuCountStr, found := podAnnotations[vcpuCountAnnotationKeyName]; found {
		var err error
		if va.VCPUCount, err = strconv.Atoi(vcpuCountStr); err != nil {
			return fmt.Errorf("error parsing cpu count for VM pod %q: %v", vcpuCountStr, err)
		}
	}

	if metaDataStr, found := podAnnotations[cloudInitMetaDataKeyName]; found {
		if err := yaml.Unmarshal([]byte(metaDataStr), &va.MetaData); err != nil {
			return fmt.Errorf("failed to unmarshal cloud-init metadata: %v", err)
		}
	}

	if userDataStr, found := podAnnotations[cloudInitUserDataKeyName]; found {
		var userData map[string]interface{}
		if err := yaml.Unmarshal([]byte(userDataStr), &userData); err != nil {
			return fmt.Errorf("failed to unmarshal cloud-init userdata: %v", err)
		}
		if va.UserDataOverwrite {
			va.UserData = userData
		} else {
			va.UserData = utils.Merge(va.UserData, userData).(map[string]interface{})
		}
	}

	va.UserDataScript = podAnnotations[cloudInitUserDataScriptKeyName]

	if sshKeysStr, found := podAnnotations[sshKeysKeyName]; found {
		if va.UserDataOverwrite {
			va.SSHKeys = nil
		}
		keys := strings.Split(sshKeysStr, "\n")
		for _, k := range keys {
			k = strings.TrimSpace(k)
			if k != "" {
				va.SSHKeys = append(va.SSHKeys, k)
			}
		}
	}

	va.CDImageType = CloudInitImageType(strings.ToLower(podAnnotations[cloudInitImageType]))
	va.DiskDriver = DiskDriverName(podAnnotations[diskDriverKeyName])

	return nil
}
<|endoftext|>