示例#1
0
    public void NamespaceCanBeNull()
    {
        var resource = new V1ClusterRole(
            apiVersion: $"{V1ClusterRole.KubeGroup}/{V1ClusterRole.KubeApiVersion}",
            kind: V1ClusterRole.KubeKind,
            metadata: new V1ObjectMeta(
                name: "the-name"));

        var key = GroupKindNamespacedName.From(resource);

        Assert.Equal("rbac.authorization.k8s.io", key.Group);
        Assert.Equal("ClusterRole", key.Kind);
        Assert.Null(key.NamespacedName.Namespace);
        Assert.Equal("the-name", key.NamespacedName.Name);
    }
示例#2
0
    public void GroupKindAndNamespacedNameFromResource()
    {
        var resource = new V1Role(
            apiVersion: $"{V1Role.KubeGroup}/{V1Role.KubeApiVersion}",
            kind: V1Role.KubeKind,
            metadata: new V1ObjectMeta(
                name: "the-name",
                namespaceProperty: "the-namespace"));

        var key = GroupKindNamespacedName.From(resource);

        Assert.Equal("rbac.authorization.k8s.io", key.Group);
        Assert.Equal("Role", key.Kind);
        Assert.Equal("the-namespace", key.NamespacedName.Namespace);
        Assert.Equal("the-name", key.NamespacedName.Name);
    }
示例#3
0
    public void GroupCanBeEmpty()
    {
        var resource = new V1ConfigMap(
            apiVersion: V1ConfigMap.KubeApiVersion,
            kind: V1ConfigMap.KubeKind,
            metadata: new V1ObjectMeta(
                name: "the-name",
                namespaceProperty: "the-namespace"));

        var key = GroupKindNamespacedName.From(resource);

        Assert.Equal("", key.Group);
        Assert.Equal("ConfigMap", key.Kind);
        Assert.Equal("the-namespace", key.NamespacedName.Namespace);
        Assert.Equal("the-name", key.NamespacedName.Name);
    }
示例#4
0
    public void NotifyWithPrimaryResourceCausesCacheEntryAndQueueItem()
    {
        var generator       = Mock.Of <IOperatorGenerator <TypicalResource> >();
        var typicalInformer = new FakeResourceInformer <TypicalResource>();
        var podInformer     = new FakeResourceInformer <V1Pod>();
        var addCalls        = new List <NamespacedName>();
        var queue           = new FakeQueue <NamespacedName>
        {
            OnAdd = addCalls.Add,
        };
        var cache = new OperatorCache <TypicalResource>();

        using var host = new HostBuilder()
                         .ConfigureServices(services =>
        {
            services
            .AddLogging()
            .AddKubernetesOperatorRuntime()
            .AddOperator <TypicalResource>(op =>
            {
                op.WithRelatedResource <V1Pod>();
                op.Configure(options => options.NewRateLimitingQueue = _ => queue);
            })
            .AddSingleton(generator)
            .AddSingleton <IResourceInformer <TypicalResource> >(typicalInformer)
            .AddSingleton <IResourceInformer <V1Pod> >(podInformer)
            .AddSingleton <IOperatorCache <TypicalResource> >(cache);
        })
                         .Build();

        var handler = host.Services.GetRequiredService <IOperatorHandler <TypicalResource> >();

        var typical = new TypicalResource
        {
            ApiVersion = $"{TypicalResource.KubeGroup}/{TypicalResource.KubeApiVersion}",
            Kind       = TypicalResource.KubeKind,
            Metadata   = new V1ObjectMeta(
                name: "test-name",
                namespaceProperty: "test-namespace")
        };

        var unrelatedPod = new V1Pod
        {
            ApiVersion = TypicalResource.KubeApiVersion,
            Kind       = TypicalResource.KubeKind,
            Metadata   = new V1ObjectMeta(
                name: "test-unrelated",
                namespaceProperty: "test-namespace")
        };

        var relatedPod = new V1Pod(

            apiVersion: TypicalResource.KubeApiVersion,
            kind: TypicalResource.KubeKind,
            metadata: new V1ObjectMeta(
                name: "test-related",
                namespaceProperty: "test-namespace",
                ownerReferences: new[]
        {
            new V1OwnerReference(
                uid: typical.Uid(),
                apiVersion: typical.ApiVersion,
                kind: typical.Kind,
                name: typical.Name())
        }));

        typicalInformer.Callback(WatchEventType.Added, typical);
        podInformer.Callback(WatchEventType.Added, unrelatedPod);
        podInformer.Callback(WatchEventType.Added, relatedPod);

        var expectedName = new NamespacedName("test-namespace", "test-name");

        Assert.Equal(new[] { expectedName, expectedName }, addCalls);

        Assert.True(cache.TryGetWorkItem(expectedName, out var cacheItem));

        Assert.Equal(typical, cacheItem.Resource);

        var related = Assert.Single(cacheItem.Related);

        Assert.Equal(GroupKindNamespacedName.From(relatedPod), related.Key);
        Assert.Equal(relatedPod, related.Value);
    }