first initial commit

added replace method

added cloud ips

updated the func params

removed whitespace at gce address

removed sample ntp.conf

removed whitespace from gce ntp address

created const var ntp type

added a period at the end of the func comment and used the const vars on the case statement.  Will finish sometime this weekend

unexported func and const type

trying to fix git email config issue

changed func param
This commit is contained in:
Simon Macklin 2020-01-09 19:58:06 +00:00
parent 1b8b0743b3
commit 2f0c33fd4e
1 changed files with 97 additions and 1 deletions

View File

@ -17,6 +17,10 @@ limitations under the License.
package model
import (
"fmt"
"io/ioutil"
"regexp"
"k8s.io/klog"
"k8s.io/kops/nodeup/pkg/distros"
"k8s.io/kops/upup/pkg/fi"
@ -32,6 +36,13 @@ type NTPBuilder struct {
var _ fi.ModelBuilder = &NTPBuilder{}
type ntpDaemon string
var (
chronyd ntpDaemon = "chronyd"
ntpd ntpDaemon = "ntpd"
)
// Build is responsible for configuring NTP
func (b *NTPBuilder) Build(c *fi.ModelBuilderContext) error {
switch b.Distribution {
@ -46,22 +57,107 @@ func (b *NTPBuilder) Build(c *fi.ModelBuilderContext) error {
return nil
}
var ntpIP string
switch b.Cluster.Spec.CloudProvider {
case "aws":
ntpIP = "169.254.169.123"
case "gce":
ntpIP = "time.google.com"
default:
ntpIP = ""
}
if b.Distribution.IsDebianFamily() {
c.AddTask(&nodetasks.Package{Name: "ntp"})
if ntpIP != "" {
bytes, err := updateNtpIP(ntpIP, ntpd)
if err != nil {
return err
}
c.AddTask(&nodetasks.File{
Path: "/etc/ntp.conf",
Contents: fi.NewBytesResource(bytes),
Type: nodetasks.FileType_File,
Mode: s("0644"),
})
}
c.AddTask((&nodetasks.Service{Name: "ntp"}).InitDefaults())
} else if b.Distribution.IsRHELFamily() {
switch b.Distribution {
case distros.DistributionCentos8, distros.DistributionRhel8:
c.AddTask(&nodetasks.Package{Name: "chrony"})
if ntpIP != "" {
bytes, err := updateNtpIP(ntpIP, chronyd)
if err != nil {
return err
}
c.AddTask(&nodetasks.File{
Path: "/etc/chrony.conf",
Contents: fi.NewBytesResource(bytes),
Type: nodetasks.FileType_File,
Mode: s("0644"),
})
}
c.AddTask((&nodetasks.Service{Name: "chronyd"}).InitDefaults())
default:
c.AddTask(&nodetasks.Package{Name: "ntp"})
if ntpIP != "" {
bytes, err := updateNtpIP(ntpIP, ntpd)
if err != nil {
return err
}
c.AddTask(&nodetasks.File{
Path: "/etc/ntp.conf",
Contents: fi.NewBytesResource(bytes),
Type: nodetasks.FileType_File,
Mode: s("0644"),
})
}
c.AddTask((&nodetasks.Service{Name: "ntpd"}).InitDefaults())
}
} else {
klog.Warningf("unknown distribution, skipping ntp install: %v", b.Distribution)
return nil
}
return nil
}
// updateNtpIP takes a ip and a ntpDaemon and will comment out
// the default server or pool values and append the correct cloud
// ip to the ntp config file.
func updateNtpIP(ip string, daemon ntpDaemon) ([]byte, error) {
var address string
var r *regexp.Regexp
var path string
switch ntpd {
case ntpd:
address = fmt.Sprintf("server %s prefer iburst", ip)
// the regex strings might need a bit more work
r = regexp.MustCompile(`pool\s\d.*[a-z].[a-z].[a-z]\siburst`)
path = "/etc/ntp.conf"
case chronyd:
address = fmt.Sprintf("server %s prefer iburst minpoll 4 maxpoll 4", ip)
// the regex strings might need a bit more work
r = regexp.MustCompile(`server\s.*iburst.*`)
path = "/etc/chrony.conf"
default:
return nil, fmt.Errorf("%s is not a supported ntp application", ntpd)
}
f, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
new := r.ReplaceAllFunc(f, func(b []byte) []byte {
return []byte(fmt.Sprintf("#commented out by kops %s", string(b)))
})
new = append(new, []byte(address)...)
return new, nil
}