Commit 3d600e0b authored by Geoff Simmons's avatar Geoff Simmons

Add the versionkey flag.

This is a key in the ConfigMap used for VCL configuration. If set,
and if the value is not empty, then the value is a version string
for the VCL config. It is used in the generation of the VCL config
name, and in the fqVersion function for VCL templates.
parent 6b6617e6
......@@ -82,6 +82,8 @@ var (
"the VCL source to be loaded, must include all other sources")
cfgMapF = flag.String("configmap", "",
"k8s ConfigMap representing Varnish sources (required)")
versionKeyF = flag.String("versionkey", "",
"key in the ConfigMap for a VCL version string")
logFormat = logrus.TextFormatter{
DisableColors: true,
......@@ -237,6 +239,7 @@ func main() {
Main: *mainF,
CfgMapNamespace: cfgMapNamespace,
CfgMapName: cfgMapName,
VersionKey: *versionKeyF,
}
vController, err := varnish.NewController(log, *homeF, tmplMap, *mainF,
......
......@@ -49,6 +49,7 @@ type SpecUpdaterConfig struct {
Main string
CfgMapName string
CfgMapNamespace string
VersionKey string
}
// SpecUpdater encapsulates updating the VCL spec from the current
......@@ -122,11 +123,10 @@ func (updater *SpecUpdater) getSvcSpec(
return
}
func (updater *SpecUpdater) getSpec(version string) (vcl.Spec, error) {
func (updater *SpecUpdater) getSpec() (vcl.Spec, error) {
updater.log.Info("Updating VCL spec from cluster config")
spec := vcl.Spec{
Version: version,
Main: updater.cfg.Main,
Created: time.Now(),
}
......@@ -168,7 +168,14 @@ func (updater *SpecUpdater) getSpec(version string) (vcl.Spec, error) {
if err != nil {
return spec, err
}
version := ""
if updater.cfg.VersionKey != "" {
if v, exists := cfgMap.Data[updater.cfg.VersionKey]; exists {
version = v
}
}
spec.Version = version
spec.CfgMap.Name = cfgMap.Name
spec.CfgMap.Namespace = cfgMap.Namespace
spec.CfgMap.Version = version
......@@ -182,7 +189,7 @@ func (updater *SpecUpdater) getSpec(version string) (vcl.Spec, error) {
// k8s cluster, and loads it as a new instance of VCL.
func (updater *SpecUpdater) Update() error {
updater.log.Info("Update signal received")
spec, err := updater.getSpec("")
spec, err := updater.getSpec()
if err != nil {
// XXX generate Warn event
updater.log.Errorf("Cannot create spec: %s", err)
......
......@@ -148,8 +148,12 @@ func (spec Spec) DeepHash() []byte {
// creation time of the Spec (since the same VCL version may be used
// with different backend configurations).
func (spec Spec) FQVersion() string {
if spec.Version != "" {
return spec.CfgMap.Namespace + "/" + spec.CfgMap.Name + "-" +
spec.Version + "-" + spec.Created.Format(RFC3339Micro)
}
return spec.CfgMap.Namespace + "/" + spec.CfgMap.Name + "-" +
spec.Version + "-" + spec.Created.Format(RFC3339Micro)
spec.Created.Format(RFC3339Micro)
}
// ConfigName generates a configuration name that is safe to use in a
......
......@@ -232,8 +232,8 @@ func TestFQVersion(t *testing.T) {
if testing.Verbose() {
t.Log("FQVersion():", fqVersion)
}
versionSlice = strings.SplitN(fqVersion, "-", 4)
if created, err := time.Parse(RFC3339Micro, versionSlice[3]); err != nil {
versionSlice = strings.SplitN(fqVersion, "-", 3)
if created, err := time.Parse(RFC3339Micro, versionSlice[2]); err != nil {
t.Fatal("FQVersion(): cannot parse timestamp", err)
} else if noversionSpec.Created != created {
t.Fatal("FQVersion(): timestamp != Spec.Created", fqVersion)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment