mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-19 05:36:08 +00:00
show server version in ark version output using ServerStatusRequest CRD
Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright 2018 the Heptio Ark contributors.
|
||||
|
||||
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 serverstatusrequest
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
arkv1api "github.com/heptio/ark/pkg/apis/ark/v1"
|
||||
)
|
||||
|
||||
type Builder struct {
|
||||
serverStatusRequest arkv1api.ServerStatusRequest
|
||||
}
|
||||
|
||||
func NewBuilder() *Builder {
|
||||
return &Builder{
|
||||
serverStatusRequest: arkv1api.ServerStatusRequest{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: arkv1api.SchemeGroupVersion.String(),
|
||||
Kind: "ServerStatusRequest",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Builder) Build() *arkv1api.ServerStatusRequest {
|
||||
return &b.serverStatusRequest
|
||||
}
|
||||
|
||||
func (b *Builder) Namespace(namespace string) *Builder {
|
||||
b.serverStatusRequest.Namespace = namespace
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) Name(name string) *Builder {
|
||||
b.serverStatusRequest.Name = name
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) GenerateName(name string) *Builder {
|
||||
b.serverStatusRequest.GenerateName = name
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) Phase(phase arkv1api.ServerStatusRequestPhase) *Builder {
|
||||
b.serverStatusRequest.Status.Phase = phase
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) ProcessedTimestamp(time time.Time) *Builder {
|
||||
b.serverStatusRequest.Status.ProcessedTimestamp.Time = time
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) ServerVersion(version string) *Builder {
|
||||
b.serverStatusRequest.Status.ServerVersion = version
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright 2018 the Heptio Ark contributors.
|
||||
|
||||
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 serverstatusrequest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
|
||||
arkv1api "github.com/heptio/ark/pkg/apis/ark/v1"
|
||||
"github.com/heptio/ark/pkg/buildinfo"
|
||||
arkv1client "github.com/heptio/ark/pkg/generated/clientset/versioned/typed/ark/v1"
|
||||
)
|
||||
|
||||
const ttl = time.Minute
|
||||
|
||||
// Process fills out new ServerStatusRequest objects and deletes processed ones
|
||||
// that have expired.
|
||||
func Process(req *arkv1api.ServerStatusRequest, client arkv1client.ServerStatusRequestsGetter, clock clock.Clock, log logrus.FieldLogger) error {
|
||||
switch req.Status.Phase {
|
||||
case "", arkv1api.ServerStatusRequestPhaseNew:
|
||||
log.Info("Processing new ServerStatusRequest")
|
||||
return errors.WithStack(patch(client, req, func(req *arkv1api.ServerStatusRequest) {
|
||||
req.Status.ServerVersion = buildinfo.Version
|
||||
req.Status.ProcessedTimestamp.Time = clock.Now()
|
||||
req.Status.Phase = arkv1api.ServerStatusRequestPhaseProcessed
|
||||
}))
|
||||
case arkv1api.ServerStatusRequestPhaseProcessed:
|
||||
log.Debug("Checking whether ServerStatusRequest has expired")
|
||||
expiration := req.Status.ProcessedTimestamp.Add(ttl)
|
||||
if expiration.After(clock.Now()) {
|
||||
log.Debug("ServerStatusRequest has not expired")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debug("ServerStatusRequest has expired, deleting it")
|
||||
if err := client.ServerStatusRequests(req.Namespace).Delete(req.Name, nil); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
default:
|
||||
return errors.Errorf("unexpected ServerStatusRequest phase %q", req.Status.Phase)
|
||||
}
|
||||
}
|
||||
|
||||
func patch(client arkv1client.ServerStatusRequestsGetter, req *arkv1api.ServerStatusRequest, updateFunc func(*arkv1api.ServerStatusRequest)) error {
|
||||
originalJSON, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
updateFunc(req)
|
||||
|
||||
updatedJSON, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
patchBytes, err := jsonpatch.CreateMergePatch(originalJSON, updatedJSON)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
_, err = client.ServerStatusRequests(req.Namespace).Patch(req.Name, types.MergePatchType, patchBytes)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Copyright 2018 the Heptio Ark contributors.
|
||||
|
||||
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 serverstatusrequest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
|
||||
arkv1api "github.com/heptio/ark/pkg/apis/ark/v1"
|
||||
"github.com/heptio/ark/pkg/buildinfo"
|
||||
"github.com/heptio/ark/pkg/generated/clientset/versioned/fake"
|
||||
)
|
||||
|
||||
func statusRequestBuilder() *Builder {
|
||||
return NewBuilder().Namespace(arkv1api.DefaultNamespace).Name("sr-1")
|
||||
}
|
||||
|
||||
func TestProcess(t *testing.T) {
|
||||
// now will be used to set the fake clock's time; capture
|
||||
// it here so it can be referenced in the test case defs.
|
||||
now, err := time.Parse(time.RFC1123, time.RFC1123)
|
||||
require.NoError(t, err)
|
||||
now = now.Local()
|
||||
|
||||
buildinfo.Version = "test-version-val"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
req *arkv1api.ServerStatusRequest
|
||||
expected *arkv1api.ServerStatusRequest
|
||||
expectedErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "server status request with empty phase gets processed",
|
||||
req: statusRequestBuilder().Build(),
|
||||
expected: statusRequestBuilder().
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(arkv1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now).
|
||||
Build(),
|
||||
},
|
||||
{
|
||||
name: "server status request with phase=New gets processed",
|
||||
req: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhaseNew).
|
||||
Build(),
|
||||
expected: statusRequestBuilder().
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(arkv1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now).
|
||||
Build(),
|
||||
},
|
||||
{
|
||||
name: "server status request with phase=Processed gets deleted if expired",
|
||||
req: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now.Add(-61 * time.Second)).
|
||||
Build(),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "server status request with phase=Processed does not get deleted if not expired",
|
||||
req: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now.Add(-59 * time.Second)).
|
||||
Build(),
|
||||
expected: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now.Add(-59 * time.Second)).
|
||||
Build(),
|
||||
},
|
||||
{
|
||||
name: "server status request with invalid phase returns an error",
|
||||
req: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhase("an-invalid-phase")).
|
||||
Build(),
|
||||
expected: statusRequestBuilder().
|
||||
Phase(arkv1api.ServerStatusRequestPhase("an-invalid-phase")).
|
||||
Build(),
|
||||
expectedErrMsg: "unexpected ServerStatusRequest phase \"an-invalid-phase\"",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
client := fake.NewSimpleClientset(tc.req)
|
||||
|
||||
err := Process(tc.req, client.ArkV1(), clock.NewFakeClock(now), logrus.StandardLogger())
|
||||
if tc.expectedErrMsg == "" {
|
||||
assert.Nil(t, err)
|
||||
} else {
|
||||
assert.EqualError(t, err, tc.expectedErrMsg)
|
||||
}
|
||||
|
||||
res, err := client.ArkV1().ServerStatusRequests(tc.req.Namespace).Get(tc.req.Name, metav1.GetOptions{})
|
||||
if tc.expected == nil {
|
||||
assert.Nil(t, res)
|
||||
assert.True(t, apierrors.IsNotFound(err))
|
||||
} else {
|
||||
assert.Equal(t, tc.expected, res)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user